自动下载URL作为HTML文件,而不是访问页面

自动下载URL作为HTML文件,而不是访问页面,html,jquery,Html,Jquery,我想要一个本地的.html文件。当我在safari中打开它时,它会自动下载一个特定的URL作为html文件 我设法得到一个.html文件来下载一个特定的URL作为html文件,使用下面的代码,遵循说明 $(“a[下载]”)。单击(函数(e){ e、 预防默认值(); $.ajax({ url:“https://cors-anywhere.herokuapp.com/“+$(this.attr(“href”), 标题:{ “X-request-With”:“true” }, 成功:功能(数据){

我想要一个本地的.html文件。当我在safari中打开它时,它会自动下载一个特定的URL作为html文件

我设法得到一个.html文件来下载一个特定的URL作为html文件,使用下面的代码,遵循说明


$(“a[下载]”)。单击(函数(e){
e、 预防默认值();
$.ajax({
url:“https://cors-anywhere.herokuapp.com/“+$(this.attr(“href”),
标题:{
“X-request-With”:“true”
},
成功:功能(数据){
log(“数据为:”,数据);
变量a=$(')


启动自动下载文件
$(函数(){
$('a[数据自动下载])。每个(函数(){
var$this=$(this);
setTimeout(函数(){
window.location=$this.attr('href');
}, 0000);
});
});
下载应该很快开始。如果没有,请单击
.

当我尝试合并这两个代码时(见下文),我得到了一个错误

undefined不是对象(评估“e.preventDefault”)


启动自动下载URL作为html文件
$(函数(){
$('a[数据自动下载])。每个(函数(){
var$this=$(this);
设置超时(函数(e){
e、 预防默认值();
$.ajax({
url:“https://cors-anywhere.herokuapp.com/“+$(this.attr(“href”),
标题:{
“X-request-With”:“true”
},
成功:功能(数据){
log(“数据为:”,数据);
变量a=$(')。


我太小,无法调试它。请帮助我。提前谢谢。

您没有使用
this
的正确范围。您已经在中定义了变量
var$this=$(this);
。每个函数中都定义了变量。但是您在setTimeout中调用了
$(this)
,它引用的setTimeout函数将未定义

另外,您不需要在
setTimeout
函数中使用
e.PreventDefault
,因为它无论如何都会运行,并且没有使用
prevent.default的默认行为

此外,我们不需要在setTimeout中定义
000
,只需放入
0
,这意味着它将在页面加载后立即运行,或者您可以设置
1000
,以便它在开始下载之前等待
1秒

工作代码笔演示:

运行下面的代码段以查看正在浏览器中下载的文件。(如果代码段未下载该文件,则表示堆栈溢出代码段正在阻止它)将此代码添加到网站中,它将正常工作

$(函数(){
$('a[数据自动下载])。每个(函数(){
var$this=$(this);
setTimeout(函数(){
$.ajax({
url:“https://cors-anywhere.herokuapp.com/“+$this.attr(“href”),//未正确调用它
标题:{
“X-request-With”:“true”
},
成功:功能(数据){
log(“数据为:”,数据);
变量a=$(')。


@halfmonhalf听到这个消息我很高兴。编码快乐!
<!DOCTYPE html>
<html>
   <body>
      <a href="https://stackoverflow.com/questions/11438864/get-html-content-from-another-site" download >a website</a>
      <script src="./jquery.js"></script>
      <script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>
      <script>
         $("a[download]").click(function(e) {
           e.preventDefault();
           $.ajax({
             url: "https://cors-anywhere.herokuapp.com/" + $(this).attr("href"),
             headers: {
               "X-Requested-With": "true"
             },
             success: function(data) {
               console.log("The data is:", data);
         
               var a = $('<a></a>');
         
               a.attr("href", window.URL.createObjectURL(new Blob([data], {
                 type: 'text/plain'
               })));
         
               a.attr("download", "page.html");
         
               $("body").append(a);
         
               a[0].click();
             }
           });
         
         });
         
      </script>
   </body>
</html>
<html>
   <head>
      <title>Start Auto Download file</title>
      <script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>
      <script>
         $(function() {
         $('a[data-auto-download]').each(function(){
         var $this = $(this);
         setTimeout(function() {
         window.location = $this.attr('href');
         }, 0000);
         });
         });
      </script>
   </head>
   <body>
      <div class="wrapper">
         <p>The download should start shortly. If it doesn't, click
            <a data-auto-download href="https://stackoverflow.com/questions/156686/how-to-start-automatic-download-of-a-file-in-internet-explorer">here</a>.
         </p>
      </div>
   </body>
</html>
<html>
   <head>
      <title>Start Auto Download URL as html file</title>
      <script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>
      <script>
         $(function() {
         $('a[data-auto-download]').each(function(){
         var $this = $(this);
         setTimeout(function(e) {
           e.preventDefault();
           $.ajax({
             url: "https://cors-anywhere.herokuapp.com/" + $(this).attr("href"),
             headers: {
               "X-Requested-With": "true"
             },
             success: function(data) {
               console.log("The data is:", data);
         
               var a = $('<a></a>');
         
               a.attr("href", window.URL.createObjectURL(new Blob([data], {
                 type: 'text/plain'
               })));
         
               a.attr("download", "page.html");
         
               $("body").append(a);
         
               a[0].click();
             }
           });
         
         }, 0000);
         });
         });
      </script>
   </head>
   <body>
      <div class="wrapper">
         <p>The download should start shortly. If it doesn't, click
            <a data-auto-download href="https://stackoverflow.com/questions/156686/how-to-start-automatic-download-of-a-file-in-internet-explorer">here</a>.
         </p>
      </div>
   </body>
</html>