JavaScript to determine the implementation code of page closure and page refresh, and javascript page refresh

Source: Internet
Author: User

JavaScript to determine the implementation code of page closure and page refresh, and javascript page refresh

Today, because the project needs to determine whether the user's behavior is disabled or refreshed when the user leaves the page.
Although there is no direct method, it can be achieved through certain skills
I have to lament the power of JavaScript !!
Take a look at the Code:
Copy codeThe Code is as follows:
Window. onunload = function (){
Var a_n = window. event. screenX-window. screenLeft;
Var a_ B = a_n> document.doc umentElement. scrollWidth-20;
If (a_ B & window. event. clientY <0 | window. event. altKey ){
Alert ('closing page activity ');
} Else {
Alert ('jump to or refresh the page ');
}
}

In fact, it is to detect the window size of the browser by leaving the page Behavior Time onunload trigger time. Based on the size, it is determined that the user is refreshing, redirecting, or disabling the behavior.

Compatible with various browsers!


JavaScript checks whether a page is closed

<Script type = "text/javascript">
Var N = document. getElementById (your_submit_Id );
Var flag = 0;
N. attachEvent ("onclick", function (){
Try {
Target_w = window. open ("bb. asp", "alredy_exit ");
If (target_cmdopener.name = "") flag = 1;
} Catch (e ){
If (e = "[object Error]")
Window. self. close ();
}
If (flag) {window. self. close ()}
Else {window. open ("bb. asp ")}
});
</Script>
This code can only identify the bb that has been opened through this page. whether asp exists or not. To meet your needs, the MS Browser has blocked the URL search function (Privacy Protection) using history, and JavaScript cannot reference non-correlated windows, if it is a desktop program, it is possible, for example, to call findWindow to find the window name. If it exists, return the window handle, and interoperability is very simple.

How to Write javascript code to prevent the website from being closed

Onbeforeunload Method

Onbeforeunload and onunload events
Onunload and onbeforeunload are all called during refresh or shutdown. You can use window. onunload in the <script> script to specify them or in <body>. The difference is that onbeforeunload is executed before onunload, which can also stop onunload execution.
Onbeforeunload is also called when the page is refreshed or closed. Onbeforeunload is called when the server is about to read a new page. At this time, it has not started reading; onunload has read the new page to be loaded from the server and is called when the current page is to be replaced. Onunload cannot prevent page updates or closures. Onbeforeunload can be used.

Only onload is executed when the page is loaded.
When the page is closed, run onbeforeunload and then onunload.
When the page is refreshed, execute onbeforeunload first, then onunload, and finally onload.

1. onbeforeunload event:
Note: currently, firefox and IE of the three major browsers support onbeforeunload events, but opera does not.
Usage:
· Object. onbeforeunload = handler
· <Element onbeforeunload = "handler "... > </Element>
Description:
When an event is triggered, a confirmation and cancellation dialog box is displayed. If you are OK, the page is left. If you cancel the event, the page continues to be canceled. Handler can set a return value as the displayed Text of the dialog box.

Triggered on:
· Close the browser window
· When you go to other pages through the address bar or Favorites folder
· Click return, forward, and refresh. When one of the home pages is displayed
· When you click a url to connect to another page
· Call any of the following events: click, document write, document open, document close, window close, window navigate, window NavigateAndFind, location replace, location reload, form submit.
· When you use window open to open a page and pass the window name on this page to the page to open.
· When the location. href value is assigned again.
· When submitting a form with the specified action through the input type = "submit" button.
It can be used in the following elements:
· BODY, FRAMESET, window
Platform support:
IE4 +/Win, Mozilla 1.7a +, Netscape 7.2 +, Firefox0.9 +
Example:
<Html xmlns = "www.w3.org/5o/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title>

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.