Javascript method to close the page: 1. Close the window without any prompt through "window.close();"; 2. Close the page through "custom_close"; 3. Through "javascript:window.opener" =null;"Close the current page.
The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.
JS summary of several ways to close the current page (window)
1. JS code to close the window without any prompts
The code is as follows:
<a href="javascript:window.opener=null;window.open('','_self');window.close();">关闭</a>
2. Custom prompt close
The code is as follows:
<script language="javascript"> // 这个脚本是 ie6和ie7 通用的脚本 function custom_close(){ if (confirm("您确定要关闭本页吗?")){ window.opener=null; window.open('','_self'); window.close(); } else{} } </script> <input id="btnClose" type="button" value="关闭本页" onClick="custom_close()" />
3.Close the current page:
The code is as follows:
<a href="javascript:window.opener=null;window.close();">关闭</a>如果是按钮则: Response.Write("<script language=\"javascript\">window.opener=null;window.close();</script>");
In this way, when you click close, the dialog box that the current window is trying to close will not pop up.
So how can the close button pop up when the user clicks the close button in the browser's maximize/minimize close button? What about the confirmation dialog box? Like this:
The code is as follows:
<body onbeforeunload="return '真的要关闭此窗口吗?'">
In this case, the onbeforeunload function will be executed when the click is closed, and a dialog box will pop up asking "Do you really want to close this window?" , click Cancel to return false, do not close, click OK to return True and close the window
So how can I pop up the OK Cancel dialog box when clicking a certain button?? If you click Cancel, the following code will not be executed. Click OK to continue executing the following code?
Write in the click of the button:
The code is as follows:
Response.Write("<script language=\javascript\">" + "if(confirm(\"确定吗?\"))"+"{window.location.href='default.aspx';}"+"else{history.back();}"+"</script>");
It means: first use the confirm function to pop up A dialog box with confirmation to cancel. If you click OK, it will return true and execute the window.location.href='default.aspx' code. If you click cancel, it will return false and execute history.back(); to return to Original page
[Recommended learning:javascript advanced tutorial】
The above is the detailed content of How to close the page in javascript. For more information, please follow other related articles on the PHP Chinese website!