I'd like to check if the current browser supports the onbeforeunload event. The common javascript way to do this does not seem to work:
if (window.onbeforeunload) {
alert('yes');
}
else {
alert('no');
}
Actually, it only checks whether some handler has been attached to the event. Is there a way to detect if onbeforeunload is supported without detecting the particular browser name?
I wrote about a more-or-less reliable inference for detecting event support in modern browsers some time ago. You can see on a demo page that "beforeunload" is supported in at least Safari 4+, FF3.x+ and IE.
Edit: This technique is now used in jQuery, Prototype.js, Modernizr, and likely other scripts and libraries.
Unfortunately kangax's answer doesn't work for Safari on iOS. In my testing beforeunload
was supported in every browser I tried exactly except Safari on IOS :-(
Instead I suggest a different approach:
The idea is simple. On the very first page visit, we don't actually know yet if
beforeunload
is supported. But on that very first page, we set up both an
unload
and a beforeunload
handler. If the beforeunload
handler fires, we set a
flag saying that beforeunload
is supported (actually beforeunloadSupported =
"yes"
). When the unload
handler fires, if the flag hasn't been set, we set the
flag that beforeunload
is not supported.
In the following we'll use localStorage
( supported in all the browsers I care
about - see http://caniuse.com/namevalue-storage ) to get/set the flag. We
could just as well have used a cookie, but I chose localStorage
because there
is no reason to send this information to the web server at every request. We
just need a flag that survives page reloads. Once we've detected it once, it'll
stay detected forever.
With this, you can now call isBeforeunloadSupported()
and it will tell you.
(function($) {
var field = 'beforeunloadSupported';
if (window.localStorage &&
window.localStorage.getItem &&
window.localStorage.setItem &&
! window.localStorage.getItem(field)) {
$(window).on('beforeunload', function () {
window.localStorage.setItem(field, 'yes');
});
$(window).on('unload', function () {
// If unload fires, and beforeunload hasn't set the field,
// then beforeunload didn't fire and is therefore not
// supported (cough * iPad * cough)
if (! window.localStorage.getItem(field)) {
window.localStorage.setItem(field, 'no');
}
});
}
window.isBeforeunloadSupported = function () {
if (window.localStorage &&
window.localStorage.getItem &&
window.localStorage.getItem(field) &&
window.localStorage.getItem(field) == "yes" ) {
return true;
} else {
return false;
}
}
})(jQuery);
Here is a full jsfiddle with example usage.
Note that it will only have been detected on the second or any subsequent page loads on your site. If it is important to you to have it working on the very first page too, you could load an iframe
on that page with a src
attribute pointing to a page on the same domain with the detection here, make sure it has loaded and then remove it. That should ensure that the detection has been done so isBeforeunloadSupported()
works even on the first page. But I didn't need that so I didn't put that in my demo.
I realize I'm a bit late on this one, but I am dealing with this now, and I was thinking that something more like the following would be easier and more reliable. This is jQuery specific, but it should work with any system that allows you to bind and unbind events.
$(window).bind('unload', function(){
alert('unload event');
});
window.onbeforeunload = function(){
$(window).unbind('unload');
return 'beforeunload event';
}
This should unbind the unload
event if the beforeunload
event fires. Otherwise it will simply fire the unload.
alert('onbeforeunload' in window);
Alerts 'true' if onbeforeunload
is a property of window
(even if it is null).
This should do the same thing:
var supportsOnbeforeunload = false;
for (var prop in window) {
if (prop === 'onbeforeunload') {
supportsOnbeforeunload = true;
break;
}
}
alert(supportsOnbeforeunload);
Lastly:
alert(typeof window.onbeforeunload != 'undefined');
Again, typeof window.onbeforeunload
appears to be 'object', even if it currently has the value null
, so this works.
Cruster,
The "beforeunload" is not defined in the DOM-Events specification, this is a IE-specific feature. I think it was created in order to enable execution to be triggered before standard "unload" event. In other then IE browsers you could make use of capture-phase "unload" event listener thus getting code executed before for example an inline body onunload event.
Also, DOM doesn't offer any interfaces to test its support for a specific event, you can only test for support of an events group (MouseEvents, MutationEvents etc.)
Meanwhile you can also refer to DOM-Events specification http://www.w3.org/TR/DOM-Level-3-Events/events.html (unfortunately not supported in IE)
Hope this information helps some
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