JavaScript onunload and onbeforeunload use summary _javascript tips

Source: Internet
Author: User
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>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.