'Some (strange?) issues about window.unload in jQuery
I'm using following code (purely for testing purposes):
$(window).bind('unload', function() {alert('unload!')});
$(window).unload(function () {alert(".unload()")});
And I have two questions:
Why it does work -- i.e. why do I see two messages? I read somewhere that attaching two event listeners to the same DOM objects does no effect; second and more listeners are ignored.
Why Firefox shows both messages in a standard, system-like message box, while all other messages (also alerts), fired in the middle of using a page, are always displayed in this ugly, title-less, annoying, message-box-like thing?
As I wrote in comment to this question, do you really thing, that blocking
alert()inunload(by Chrome / WebKit) is a good idea? Replacing alert with a console.log (as suggested there) won't give you much useful effect, as console (at least in Chrome) is being cleared, right after you refresh page or navigate to another one. You can see traces of your messages written to console some mili-seconds before they actually disappear, so I would rather find it useless. Disabling alert in unload isn't best idea. It is hard to do some serious testings and "awful" pages can do easy workaround by displaying own, "annoying" message, not using alert (jQuery UI, some div, etc.).
These questions are purely to satisfy my hunger to know ALL the answers! :] So, don't treat it to seriously (like doing some extra tests, fiddle examples etc.). Just, write what you know or feel about this.
Solution 1:[1]
Binding multiple event handlers for the same event (with jQuery) most certainly will cause both to be fired. You may be thinking about successive assignments to "onfoo" element attributes, but that's not the mechanism jQuery (or any other modern framework I know of) uses internally.
Browsers show alert() messages however they want. It's not under control of the code on the page.
Finally, disallowing alert() in "unload" event handlers is a good idea for users. You can control whether the debugging console clears across page loads.
Solution 2:[2]
(at least in Chrome) is being cleared, right after you refresh page or navigate to another one
Set up your debugger to preserve the console! Learn about the features of the tools you use every day.
- Chrome - right click in the console tab, option in the context menu
- Firebug - button in the console
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pointy |
| Solution 2 | epascarello |
