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?
Add the download
attribute to the a tag, which seems to prevent onbeforeunload from firing:
<a download href="mysite.com"></a>
From this answer.
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).
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