Setting onbeforeunload on body element in Chrome and IE using jQuery Setting onbeforeunload on body element in Chrome and IE using jQuery google-chrome google-chrome

Setting onbeforeunload on body element in Chrome and IE using jQuery


you cannot abort page unload by returning false. you must return string that will be shown to user in a message box, and he decides if he want to leave or stay on the page (by selecting either 'OK' or 'Cancel' button), so you need to write your code like this:

 window.onbeforeunload = function() {    return "Are you sure you want to leave this page bla bla bla?"; // you can make this dynamic, ofcourse... };


try this

  <script type=\"text/javascript\">        var dont_confirm_leave = 0; //set dont_confirm_leave to 1 when you want the user to be able to leave withou confirmation        var leave_message = 'You sure you want to leave?'        function goodbye(e)         {            if(dont_confirm_leave!==1)            {                if(!e) e = window.event;                //e.cancelBubble is supported by IE - this will kill the bubbling process.                e.cancelBubble = true;                e.returnValue = leave_message;                //e.stopPropagation works in Firefox.                if (e.stopPropagation)                 {                    e.stopPropagation();                    e.preventDefault();                }                //return works for Chrome and Safari                return leave_message;            }        }       window.onbeforeunload=goodbye;    </script>


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