Javascript determines whether the webpage is closed or refreshed, and javascript determines
The principle is to detect the window size of the browser by leaving the page Behavior Time onunload trigger time, and determine whether the user is refreshing, redirecting to or closing the behavior program based on the size.
The 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 or refresh page activity ');}}
This method is useful when you use the close button in the upper-right corner of the browser, but it is disabled on the tab and on the taskbar.
The js tag only contains the onloadonunloadonbeforeunload event, but does not have the onclose event.
The onunload Event is executed no matter whether the page is closed or refreshed.
How can I capture and close the page?
Only onload is executed when the page is loaded.
When the page is closed, only onunload is executed.
When the page is refreshed, execute onbeforeunload first, then onunload, and finally onload.
In this way, we can add a tag in onbeforeunload to judge the tag in onunload to determine whether the page is actually closed.
More compatible with ff
The Code is as follows:
<Mce: script type = "text/javascript"> <! -- Function close (evt) // author: sunlei {var isIE = document. all? True: false; evt = evt? Evt :( window. event? Window. event: null); if (isIE) {// IE browser var n = evt. screenX-window. screenLeft; var B = n> document.doc umentElement. scrollWidth-20; if (B & evt. clientY <0 | evt. altKey) {// alert ("close rather than refresh"); window. location. href = ".. /include/logout. php ";} else {// alert (" Refresh rather than close "); return false ;}} else {// Firefox if(document.doc umentElement. scrollWidth! = 0) {// alert ("Refresh rather than close"); // window. location. href = "report_list.php? Ss = 1 "; return false;} else {alert (" close rather than refresh "); // window. location. href =" repost_list.php? Ss = 0 "; // alert (" bbbbbbbbb ") ;}}// --> </mce: script> <BODY onunload =" close (event); ">
The method above cannot determine the browser with multiple tabs, such as 360 or ie8. Let's look at it again.
The Code is as follows:
Function CloseOpen (event) {if (event. clientX <= 0 | event. clientY <0) {// obtain the current time var date = new Date (); // set the date to the past time alert ("Close web page"); date. setTime (date. getTime ()-10000); // Delete the cookie userId document. cookie = "zhuangtao; expire =" + date. toUTCString (); document. cookie = "quanxianzifucuan; expire =" + date. toUTCString (); document. cookie = "quanxian; expire =" + date. toUTCString (); s0 + = "Close the window! "; Sw = 1; onbeforeunload (); // window. event. returnValue = 'Close the browser and exit the system. ';} else {alert ("Refresh or exit") ;}} var currentKeyCode =-1; function document. onkeydown () {// All subordinate pages in this window must contain the top function. currentKeyCode = event. keyCode;} function onbeforeunload () {var sw = 0, s0 = ""; if (currentKeyCode = 116) {s0 + = "refresh the window! (F5) ";} else {if (event. altKey) & (currentKeyCode = 115) {s0 + =" Close the window! (Alt + F4) "; sw = 1; // obtain the current time var date = new Date (); // set date to the past time alert ("Close Window"); date. setTime (date. getTime ()-10000); // Delete the cookie userId document. cookie = "zhuangtao; expire =" + date. toUTCString (); document. cookie = "quanxianzifucuan; expire =" + date. toUTCString (); document. cookie = "quanxian; expire =" + date. toUTCString ();} else {if (event. clientX> 0) & (event. clientX <document. body. clientWidth) {s0 + = "Refresh window Port! ";}Else {// get the current time var date = new Date (); // set the date to the past time alert (" Close web page "); date. setTime (date. getTime ()-10000); // Delete the cookie userId document. cookie = "zhuangtao; expire =" + date. toUTCString (); document. cookie = "quanxianzifucuan; expire =" + date. toUTCString (); document. cookie = "quanxian; expire =" + date. toUTCString (); s0 + = "Close the window! "; Sw = 1 ;}}if (sw = 1) {event. returnValue = "" ;}else {currentKeyCode =-1 ;}< body onunload = "CloseOpen (event)" ></body>
The above can only be disabled in the taskbar, which can basically meet our requirements.
The above content is the javascript introduced in this article to determine whether the webpage is closed or refreshed. I hope you will like it.