In my chat application I need to get confirmation from user, when my application closes.
So I used the window.onbeforeunload
for confirmation alert and window.onunload
for logout()
.
But both functions are working in IE and Chrome. (Application works fine)
window.onbeforeunload
not working in Opera and my message will not get displayed in Firefox.
window.onunload
not working in Safari, Opera and Firefox.
My JavaScript code will be:
// Used for confirmation, to closing the window window.onbeforeunload = function () { return "Are you sure want to LOGOUT the session ?"; }; // Used to logout the session, when browser window was closed window.onunload = function () { if((sessionId != null)&&(sessionId!="null")&& (sessionId != "")) logout(); };
I also tried the same function with JQuery,
<script type="text/javascript"> $(window).on('beforeunload', function() { return 'Are you sure want to LOGOUT the session ?'; }); $(window).unload(function() { if ((sessionId != null) && (sessionId != "null") && (sessionId != "")) { logout(); } }); </script>
onbeforeunload Below are my findings on the iPad; Using window. onunload , I am able to get an alert when user navigates to a different page from myPage. html (either by clicking on some link or doing a Google search while on myPage.
The onbeforeunload event occurs when the document is about to be unloaded. This event allows you to display a message in a confirmation dialog box to inform the user whether he/she wants to stay or leave the current page. The default message that appears in the confirmation box, is different in different browsers.
The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible and the event is still cancelable at this point. This event enables a web page to trigger a confirmation dialog asking the user if they really want to leave the page.
Cancelable: The beforeunload event can be canceled by user interaction: // by https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload#Example window. addEventListener("beforeunload", function(event) { event. preventDefault(); // Cancel the event as stated by the standard.
Unfortunately, the methods you are using are unsupported in those browsers. To support my answer (this unsupportive behaviour) I have given links below.
onbeforeunload
and onunload
not working in opera
... to support this
onbeforeunload in Opera
http://www.zachleat.com/web/dont-let-the-door-hit-you-onunload-and-onbeforeunload/
Though the onunload
event doesn't work completely, you can use onunload
to show a warning if a user clicks a link to navigate away from a page with an unsaved form.
onunload
not working in safari
... to support this
https://www.webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/
You could rather try using the pagehide
event in the safari browser in lieu of onunload
.
onunload
not working in firefox
... to support this
https://bugzilla.mozilla.org/show_bug.cgi?id=681636
They are yet to come up with a solution in FF too
Wish you good luck cheers.
Here is the working solution for ie, firefox and chrome:
var myEvent = window.attachEvent || window.addEventListener; var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; /// make IE7, IE8 compitable myEvent(chkevent, function(e) { // For >=IE7, Chrome, Firefox var confirmationMessage = 'Are you sure to leave the page?'; // a space (e || window.event).returnValue = confirmationMessage; return confirmationMessage; });
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