javascript - Download file on page load not working - Stack Overflow

I've been trying to get a file to download when the page loads by doing this:<div onload="

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=".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?

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./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?

Share Improve this question edited Aug 14, 2015 at 19:52 Sarath Chandra 1,88621 silver badges40 bronze badges asked Aug 14, 2015 at 18:51 user3777369user3777369 671 gold badge2 silver badges10 bronze badges
Add a ment  | 

2 Answers 2

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

This will execute your function right after instantiation.

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743762376a4502866.html

相关推荐

  • javascript - Download file on page load not working - Stack Overflow

    I've been trying to get a file to download when the page loads by doing this:<div onload="

    6天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信