I am trying to write code that opens a clean page that downloads a file when I open it. My problem is that I didn't find how to do it automatically. I only find this code:
<!DOCTYPE html>
<html>
<body>
<a href="content/file.jpg" download > jpg link </a>
</body>
</html>
Without using a backend language, you can add this jQuery to your HTML document.
<script type="text/javascript">
$(document).ready(function () {
$("a").click();
});
</script>
The link will be automatically clicked as soon as the document has loaded.
I hope this will works all the browsers. You can also set the auto download timing.
<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');
}, 2000);
});
});
</script>
</head>
<body>
<div class="wrapper">
<p>The download should start shortly. If it doesn't, click
<a data-auto-download href="auto-download.zip">here</a>.</p>
</div>
</body>
</html>
Demo Here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With