1

I have a web page which has a ajax modal popup being loaded inside it.When the modal popup is there we need to restrict the user from closing the browser.we have handled the onbeforeunload event but it showed a popup "are you sure you want ot leave this page".How to supress this popup and stop the page from closing.

<script>
window.onbeforeunload=function() {
window.alert("Hai");return false;
}
</script>
user2239309
  • 11
  • 1
  • 2

1 Answers1

1

Unfortunately you can never prevent a browser window from being closed as that would lead to all kinds of abuse (imagine a porn site opening a window you cannot close).

The onbeforeunload eventhandler should return a string that could explain to the user what will happen if he closes the window, as the returned string is shown in the confirmation box. E.g:

window.onbeforeunload = function() { 
    return "If you close the window your unsaved changes will be lost!";
}

That string combined with the non-overridable confirmation dialog with OK and Cancel buttons will hopefully steer the user into making the right decision.

krisku
  • 3,916
  • 1
  • 18
  • 10