I have an onbeforeunload
event that's supposed to get triggered any time a user goes to a new page. It works well enough, but I've found that it also gets triggered in Chrome any time a user downloads a file from the page they're on.
I'd like to be able to tell if the event is getting fired because it's being triggered by a file download. What's the best way to do that?
EDIT: As a clarification, I don't own the site that I'm listening to onbeforeunload
on. The event is listened to by a Javascript snippet that's being installed on 3rd party sites.
If you add download="[FILENAME]" to the a tag, it seems to prevent onbeforeunload from firing:
This is a much simpler solution. You can leave off the filename and just say 'download' to use the default filename. Let me point out this has the side effect of forcing redownload instead of using cache. I think this was added to chrome and ff in 2012. Not sure about safari or ie support.
A javascript work around
That's the only "clean" work around I could think of & it seems to work just fine.
Some more code in your question showing how you actually use the "onbeforeunload" would be great.
But I will assume you are using something like the code below for the cursor "loading..." animation.
The solution
1st step:
2nd step:
Note that I use
$('html').addClass('cursor-loading-progress');
as this is the expected CSS in my example but you can just do anything you like at this point.Another couple of work arounds could be: