简体   繁体   中英

Javascript Class using the window.onunload event

I have a javascript class with methods and properties.

Is it possible to wire this class up to a pageunload event?

I am trying to call a method in my class when the user moves pages.

How can I do this.

Thanks

Returning a string from the onbeforeunload event handler will show a dialog box.

<html>
<head>
</head>
<body>
test
<script>
window.onbeforeunload = function () {
   return "*** Warning: changes are not saved! ***";
};
</script>
</body>
</html>

In internet explorer this dialog looks like this:

---------------------------
Windows Internet Explorer
---------------------------
Are you sure you want to navigate away from this page?

*** Warning: changes are not saved! ***

Press OK to continue, or Cancel to stay on the current page.
---------------------------
OK   Cancel   
---------------------------

Your text will be sandwiched between some browser specific text, but that is the only way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM