Call JS functions on browser tab close

enter image description here I need to call the JS function when the user closes the browser tab. the problem is that I want this to happen only when the user closes the browser. There is no need to refresh the page, navigate links, click the submit button and return. I have tried the code below jQuery so far.

$(window).bind( "beforeunload", function() { alert("don't close me"); return false; } ) $('form').submit(function() { jQuery(window).unbind("beforeunload"); }); 

does not work. can anyone answer for this. are there any other js tools than jquery for this?

And if I call my "beforeunload" function, the following message will appear. I do not want to show this message, and my function should work. I tried to give e.preventDefault. but it does not call my function either when I used it. can anyone suggest something. Thanks.

0
javascript jquery
source share
3 answers

I agree with the comments that this is bad practice, but I can not resist the call to try to answer the question.

The only way I can do this is to use onbeforeunload , which you already do. But you need to turn off this alert when someone moves far from the page in other ways.

 var show_close_alert = true; $("a").bind("mouseup", function() { show_close_alert = false; }); $("form").bind("submit", function() { show_close_alert = false; }); $(window).bind("beforeunload", function() { if (show_close_alert) { return "Killing me won't bring her back..."; } }); 

This is not perfect, as there are ways to close the browser without seeing a warning (for example, by clicking the link, immediately clicking Stop and then closing the browser), but it can be as close as possible.

Here's the fiddle .

But please ... do not do this.

+3
source share

JSFiddle uses the following to warn the user that they are trying to close their modified script without saving:

 window.onbeforeunload = function(){ if (window.editorsModified){ return "You've modified your fiddle, reloading the page will reset all changes." } }; 

This works reliably ( http://jsfiddle.net/MaxPRafferty/b6uEF/show/ , latest Chrome, FF, IE10), but will always prompt the user. You cannot force the page to return false.

0
source share

onunload (or onbeforeunload ) cannot redirect the user to another page. This is for security reasons.

If you want to show the invitation before the user leaves the page, use onbeforeunload:

 window.onbeforeunload = function(){ return 'Are you sure you want to leave?'; }; 

Or using jQuery:

 $(window).bind('beforeunload', function(){ return 'Are you sure you want to leave?'; }); 

It will simply ask the user if they want to leave the page or not, you cannot redirect them if they want to stay on the page. If they leave, the browser will go where they told him to leave.

You can use onunload to perform operations before the page is unloaded, but you cannot redirect from there (Chrome 14+ blocks warnings inside onunload):

 window.onunload = function() { alert('Bye.'); } 

Or using jQuery:

 $(window).unload(function(){ alert('Bye.'); }); 
0
source share

All Articles