user3777369
user3777369

Reputation: 67

Download file on page load not working

I've been trying to get a file to download when the page loads by doing this:

<div onload="download()"></div>
<a id="dl" style="display: none" 
    href="http://www.miniclip.com/games/base-jumping/en/base_jumping_miniclip.swf" 
    download>Download</a>

<script>
function download(){
   document.getElementById('dl').click();
}
</script>

What it's supposed to do is trigger a function when the page loads that "clicks" the hidden download link. However, this method isn't working and I don't know why.

Anybody know why?

Upvotes: 2

Views: 13776

Answers (2)

voidzero
voidzero

Reputation: 594

<script>
window.location.href = 'http://www.miniclip.com/games/base-jumping/en/base_jumping_miniclip.swf';
</script>

Upvotes: 2

Paul T. Rawkeen
Paul T. Rawkeen

Reputation: 4114

<script>
(function download() {
    document.getElementById('dl').click();
})()
</script>

This will execute your function right after instantiation.

Upvotes: 7

Related Questions