JS distinguishes whether the browser is closed or refreshed __js

Source: Internet
Author: User
Onunload,onbeforeunload are called at refresh or shutdown, and can be specified in <script> scripts by window.onunload or in <body>. The difference is that onbeforeunload executes before onunload, and it can also prevent onunload from executing.

    onbeforeunload is also called when the page is refreshed or closed, onbeforeunload is on the way to the server to read the new page when the call, this time has not started reading; OnUnload has read the new page that needs to be loaded from the server and is 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. Have to do a test system, involving to prevent users to exit the test halfway (intentionally or unintentionally), the code is as follows: Window.onbeforeunload = Onbeforeunload_handler; Window.onunload = Onunload_handler; function Onbeforeunload_handler () {var warning= "confirm exit?"; return warning; function Onunload_handler () {var warning= "thank you"; alert (warning);} This code executes correctly on both FF and IE. when you click the Close button, the Obbeforeunload event is triggered first. Do not perform onload event when clicking Negation. Usually used in the cancellation of the session and so on landing information .... Here's a good tutorial for ACTIONSCRIPT3: Http://gskinner.com/talks/as3workshop writes using the OnUnload event to determine whether the browser refreshes or closes the window function Closeopen ( Event) {if (event.clientx<=0 && event.clienty<0) {alert ("Off");} else {alert ("Refresh or Leave");} Another way to judge the refresh and shutdown Window.onbeforeunload = function ()//author:meizz {var n = window.event.screenx-window.screenleft; var b = n > Docu ment.documentelement.scrollwidth-20; if (b && Window.event.clientY < 0 | | window.event.altKey) {ALERT ("is closed rather than refreshed"); Window.event.returnValue = ""; Here you can place the action code you want to do}else {alert ("is refreshed rather than closed");} Blog Source: http://www.javaeye.com/topic/269213

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.