Home >Web Front-end >JS Tutorial >How to close the page in javascript

How to close the page in javascript

藏色散人
藏色散人Original
2021-04-27 09:39:366532browse

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.

How to close the page in javascript

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(&#39;&#39;,&#39;_self&#39;);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(&#39;&#39;,&#39;_self&#39;);
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 &#39;真的要关闭此窗口吗?&#39;">

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=&#39;default.aspx&#39;;}"+"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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn