html - 自动将 URL 下载为 HTML 文件,而不是访问页面
问题描述
我想要一个本地的 .html 文件。当我在 safari 中打开它时,它会自动将某个 URL 下载为 html 文件。
我设法获得一个 .html 文件以将某个 URL 下载为 html 文件,使用下面的代码,按照此处的说明进行操作。
<!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 文件以自动访问 URL,使用下面的代码,按照此处的说明进行操作。
<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>
当我尝试合并两个代码时(见下文),我收到错误
undefined 不是对象(评估“e.preventDefault”)
.
<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>
我太初级,无法调试它。请帮我。提前致谢。
解决方案
您没有使用正确的this
. 您已经在 .each 函数中定义了变量var $this = $(this);
,但是您在调用$(this)
的 setTimeout 中引用了未定义的 setTimeout 函数。
此外,您无需e.PreventDefault
在setTimeout
函数中使用,因为它无论如何都会运行,并且没有默认行为可供使用prevent.Default
此外,我们不需要000
在 setTimeout 中定义 just put0
这意味着它会在页面加载后立即运行,或者您可以设置它在开始下载之前1000
等待。1 seconds
工作Codepen演示:https ://codepen.io/alwayshelping/pen/NWxmERQ
运行下面的代码片段以查看正在浏览器中下载的文件。(如果该代码段未下载文件,则意味着堆栈溢出代码段正在阻止它)将此代码添加到网站中,它将正常工作。
$(function() {
$('a[data-auto-download]').each(function() {
var $this = $(this);
setTimeout(function() {
$.ajax({
url: "https://cors-anywhere.herokuapp.com/" + $this.attr("href"), //this was not called properly
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();
}
});
}, 0);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<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>
</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>
推荐阅读
- javascript - 从插值内的属性中提取数字
- reactjs - history.push() 正在更新 url 但不重新渲染组件
- amazon-web-services - AWS lambda,C# 异步邮件发送问题 - MailJet API
- javascript - 如何使用带有axios和vuejs的url参数动态查询for循环
- vba - 在 VBA 中检查内存中的 2 个区域是否相同 - 错误的调用约定
- c++11 - 如何使用按位操作在“uint16_t”上存储和读回多个数值?
- django - 在基于类的视图中获取帖子 ID
- java - 使用java更改壁纸
- jupyter-notebook - 访问 Jupyter notebook 菜单栏的快捷键/组合键
- hibernate - Quarkus:使用 multitenant=DATABASE 时未指定租户标识符