Javascript checks whether the window is closed before the current window is closed.

Source: Internet
Author: User
Tags microsoft frontpage

Javascript checks whether the window is closed before the current window is closed.

Check whether the current window is closed before closing the current window

<Pre name = "code" class = "html"> <pre name = "code" class = "html"> <HTML> <HEAD> <script Language = "JavaScript"> window. onbeforeunload = function (event) {alert ("222"); // IE9 will be executed here, and CHROME will not execute // if (event. clientX> document. body. clientWidth & event. clientY <0 | event. altKey) {// if it is a refresh, RETURN is not prompted. "Are you sure you want to close the window? "; //} Var aa; var intervalVar; function showClose () {console. log ("wait and val is:" + aa. closed); clearInterval (intervalVar);} function loadform () {aa0000000000000000open('foo.html ', 'windowname', "width = 200, height = 200, scrollbars = no"); console. log ("check close before op and val is:" + aa. closed); // the current window is not closed, and the result is false aa. close (); <span style = "white-space: pre"> </span> // call the console to close the window. log ("not in wait Nd val is: "+ aa. closed); // aa. close is being called, and the result is false intervalVar = setInterval (showClose, 100) ;}; // checks whether the subwindow is closed cyclically. In fact, setTimeout is also acceptable, however, the value must be greater than the value of function unloadform () {alert ("2! ") ;}</Script> </HEAD> <BODY OnLoad =" loadform () "OnUnload =" unloadform () "> </BODY> </HTML>

In ie9, if the document is refreshed, both unloadform and onbeforeunload will be executed. If the page is closed, only the onbeforeunload event will be executed.

It is worth noting that onunload does not run when the page is closed. It is estimated that this function is a browser built-in event and cannot be redefined.

In chrome, if the document is refreshed, both unloadform and onbeforeunload will be executed. If the page is closed, only onbeforeunload will be executed. It is worth noting that the line with comments will not be executed.

If window. open Opens a subwindow, you can use the window. closed attribute to check whether the subwindow is closed.

<HTML> <HEAD> <script Language = "JavaScript"> var aa; var intervalVar; function showClose () {console. log ("wait and val is:" + aa. closed); clearInterval (intervalVar);} function loadform () {aa0000000000000000open('foo.html ', 'windowname', "width = 200, height = 200, scrollbars = no"); console. log ("check close before op and val is:" + aa. closed); // the current window is not closed, and the result is false aa. close (); // call the console to close the window. log ("not in wait and v Al is: "+ aa. closed); // aa. close is being called, and the result is false intervalVar = setInterval (showClose, 100) ;}; // checks whether the subwindow is closed cyclically. In fact, setTimeout is also acceptable, however, the value must be greater than the value of function unloadform () {alert ("2! ") ;}</Script> </HEAD> <BODY OnLoad =" loadform () "OnUnload =" unloadform () "> <a href =" test.htm "> call </a> </BODY> </HTML>

When the parent window is closed, the code for window. open is automatically closed.

<HTML> <HEAD> <script Language = "JavaScript"> var aa; var intervalVar; window. onbeforeunload = function (event) {aa. close (); return "hello";} function loadform () {aa000000000000open('test.htm', 'windowname', "width = 200, height = 200, scrollbars = no ");}; function unloadform () {alert ("2! ") ;}</Script> </HEAD> <BODY OnLoad =" loadform () "OnUnload =" unloadform () "> <a href =" test.htm "> call </a> </BODY> </HTML>

How does javascript refresh a window when it closes another window?

// Close the current page
Window. close ();
// Refresh
Window. opener. location. reload ();

Javascript code for closing the window

<Script language = "JavaScript">
Function geonado ()
{
Window. open ('index. asp ', '', 'fullscreen'); // jump to another window and display it in full screen.
Window. opener = null; // window disabling prompt
Window. close (); // close the window automatically
}
</Script>
</HEAD>
<BODY onload = "geonado ()">
</BODY>

<Script language = "javascript">
Function clock () {I = I-1
Document. title = "this window will be closed automatically after" + I + "seconds! ";
If (I> 0)
{SetTimeout ("clock ();", 1000 );}
Else
{
Window. opener = null;
Self. close ();
}
}
Var I = 5
Clock ();
</Script>

<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Meta name = "GENERATOR" content = "Microsoft FrontPage 4.0">
<Meta name = "ProgId" content = "FrontPage. Editor. Document">
<Title> New Page 1 </title>
<! --
By webjx.com (2005-5-3)
-->
</Head>
<Body>
<Script language = "JavaScript">
<! --
Function CloseWin ()
{
Var ua = navigator. userAgent
Var ie = navigator. appName = "Microsoft Internet Explorer "? True: false
If (ie ){
Var IEversion = parseFloat (ua. substring (ua. indexOf ("MSIE") + 5, ua. indexOf (";", ua. indexOf ("MSIE "))))
If (IEversion <5.5 ){
Var str = '<object id = noTipClose classid = "clsid: ADB880A6-D8FF-11CF-9377-00AA003B7A11">'
Str + = '<param name = "Command... the remaining full text>

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.