I would like to have a local .html file. When I open it in safari, it will automatically download a certain URL as html file.
I manage to get a .html file to download a certain URL as html file, using below code, following instructions here.
<!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>
I also manage to get a .html file to automatically visit a URL, using below code, following instructions here.
<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>
When I try to combine both code (see below), I get error
undefined is not an object (evaluating '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>
I am too junior to debug it. Please help me. thanks in advance.