javascript - Calling JS functions on browser tab close -
i need call js function when user closing browser tab. problem want happen when user closing browser. no need happen page refresh, link navigation,form submit , button press. tried below jquery code far.
$(window).bind( "beforeunload", function() { alert("don't close me"); return false; } ) $('form').submit(function() { jquery(window).unbind("beforeunload"); });
its not working. can answer this. there other js tools jquery available this?.
and if call function "beforeunload" event, above message coming. don't want show message , function has worked. tried giving e.preventdefault. it's not calling function when used that. can suggest something. thanks.
i agree comments bad practice, can't resist call attempt answering question.
the way can think of accomplish use onbeforeunload
, you're doing. need way of disabling alert when navigates away page other means.
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 back..."; } });
it's not foolproof, in there ways close browser without seeing alert (like clicking link, hitting stop immediately, , closing browser), may close can get.
here's fiddle.
but please... don't this.
Comments
Post a Comment