Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download binary without triggering onbeforeunload

I want to kick off a file download for a user when he clicks a link, but I have an onbeforeunload handler that I don't want to get invoked when the download begins. To downloads, I currently have an <a> with the href set to the file location but clicking it results in onbeforeunload being invoked in Chrome (not in FF, though).

I know I can set a private flag and check that in the onbeforeunload handler, but is there some way to kick off the download using ajax? I still want the user to see the usual dialogs when they download the file (Open/Save etc).

Ideas?

like image 642
psychotik Avatar asked Mar 16 '10 04:03

psychotik


2 Answers

Add the download attribute to the a tag, which seems to prevent onbeforeunload from firing:

<a download href="mysite.com"></a>

From this answer.

like image 77
Felix Avatar answered Oct 03 '22 18:10

Felix


I know this answer is really late, but there's a simple solution. Create an iframe dynamically, and set the "src" of it to your download url via JavaScript. This'll kick off the download without triggering the unload event (I think).

like image 39
jvenema Avatar answered Oct 03 '22 19:10

jvenema