ntsue
ntsue

Reputation: 2395

How to cancel browser close window?

Is it possible to cancel the browser close window event? Or at least add a message prompting the user whether they are sure they want to leave the page? I tried doing this with the beforeunload event, but it still closes the browser. I am looking for something similar to Microsoft's exchange website.

Thanks!

enter image description here

Upvotes: 1

Views: 11392

Answers (5)

peopleopinion
peopleopinion

Reputation: 11

window.onbeforeunload

Will work on Safari, IE, Firefox, Chrome, but not Opera.

Upvotes: 1

Liam
Liam

Reputation: 29694

a ha!

It appears there's a supported JQuery solution

   $(window).unload(function() 
{
  //...
});

http://www.webhostingtalk.com/showthread.php?t=927093

this should be cross browser supported...hopefully....

Upvotes: 2

Misha Ts
Misha Ts

Reputation: 431

use onbeforeunload event http://msdn.microsoft.com/en-us/library/ms536907%28VS.85%29.aspx

<HTML>
<head>
<script>
function closeIt()
{
  return "Any string value here forces a dialog box to \n" + 
         "appear before closing the window.";
}
window.onbeforeunload = closeIt;
</script>
</head>
<body>
  <a href="http://www.microsoft.com">Click here to navigate to 
      www.microsoft.com</a>
</body>
</html>

Upvotes: 0

user1256042
user1256042

Reputation:

Here is an Html Code:

<html>
<head>
<script language="javascript" type="text/javascript">
window.onbeforeunload = askConfirm;
function askConfirm(){
        return "You have unsaved changes.";
        }
</script>
</head>
<body>
</body>
</html>

Paste this code into the text area on this page: http://www.htmlpreview.richiebrownlee.com press the button, then exit out of the page that comes up..

Upvotes: 3

Teja Kantamneni
Teja Kantamneni

Reputation: 17472

onbeforeunload should work fine. Bind this on body/window attribute of the html. For more details check this detailed blog post, blog

Upvotes: 0

Related Questions