onBeforeUnload is also invoked when the page is refreshed or closed, onbeforeunload is about to go to the server to read the new page when the call is not started, and OnUnload has read from the server to load the new page, Called when the current page is about to be replaced. OnUnload is not able to prevent pages from being updated and closed. And onbeforeunload can do it. Once did an examination system, involves prevents the user to exit the test halfway (intentionally or unintentionally), the code is as follows:
Copy Code code as follows:
<body onbeforeunload= "Checkleave ()" >
<script>
function Checkleave () {
Event.returnvalue= "decide to give up the exam?" (The examination is void, does not record the result) ";
}
</script>
This allows users to confirm whether to exit the examination room, in fact, Blogjava in the user to write a blog, if not saved and jump to other pages, there will be a confirmation of the hint (prevent misoperation), but also used to onbeforeunload.
It can also be used to close the session when the page is closed, as follows (note: window.screenleft > 10000来 to distinguish between shutdown and refresh operations):
Copy Code code as follows:
<body onbeforeunload= "closesession ()" >
<script>
Func tion CloseSession () {
//close session at Refresh (not close session)
if (window.screenleft>10000) {
///close session (Ajax can be used) br>}
}
</script>