0

I am using the following code to warn the user when he closes the browser.The code is working fine in Chrome.But its not working properly in IE(version 9) and in firefox(version 17).I mean some of the button clicks and hyperlink clicks shows the alert in firefox as well as in ie.Also some clicks in the kendo grid too.And this problem is not occuring in chrome.Its working fine every where in the application.

var warningSuppressionTime = 0;
$(document).ready(function() {
   window.onbeforeunload = unloadPage;
   $("form").submit(dontWarn);
   $("button").on('click',dontWarn);
   $("a").on('click',dontWarn);
   $(".conten_wrp").on('click',dontWarn);//A common class in all pages where all the html is placed.
});

function unloadPage(){
    if (+new Date() - warningSuppressionTime > 1000) {
        return "Your changes will not be saved.";
    }
}
function dontWarn() {
    warningSuppressionTime = +new Date();
}

I am using kendo grid in my application.The click on buttons and <a> are also not working i the grid also.Can somebody suggest a solution. Also tried the solution mentioned in this question.But same result.

Community
  • 1
  • 1
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
  • Are those elements triggering the non-wanted alert created dynamically? – Teemu Aug 21 '13 at 05:57
  • The function unloadPage returns a string, what is meant to happen to this string? – andrewb Aug 21 '13 at 06:01
  • I couldn't reproduce your issue, except when creating a clickable element dynamically. Please notice, that even `$(element).html(some_string)` creates new nodes/elements, no matter how you have created the string. @andrew-buchan Please read some documentation of [`onbeforeunload`](https://developer.mozilla.org/en-US/docs/Web/API/window.onbeforeunload). – Teemu Aug 21 '13 at 06:32

1 Answers1

0

Make sure your code is wrapped in

$(document).ready(function()
{

});
andrewb
  • 2,995
  • 7
  • 54
  • 95