Home >Web Front-end >JS Tutorial >Summary of several ways to close the current page (window) in js

Summary of several ways to close the current page (window) in js

PHP中文网
PHP中文网Original
2017-03-28 13:58:452895browse

JS summary of several ways to close the current page (window), friends in need can refer to it

1. JS code to close the window without any prompt

The code is as follows:

<a href="javascript:window.opener=null;window.open(&#39;&#39;,&#39;_self&#39;);window.close();">关闭</a>

2. Custom prompt is closed

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:

<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 do you do that? When the user clicks the close button in the maximize or minimize close button of the browser, a closing confirmation dialog box will pop up? Like this:

The code is as follows:

<body onbeforeunload="return &#39;真的要关闭此窗口吗?&#39;">

In this case, click close When the onbeforeunload function is executed, a dialog box pops up asking whether you really want to close this window. Click Cancel to return false, do not close, click OK to return True to close the window

So how can you click a button? What about the confirmation and cancellation dialog box that pops up? If you click Cancel, the following code will not be executed. If you click OK, it will continue to execute the following code?

Write in the button click:

The code is as follows:

Response.Write("<script language=\javascript\">" + "if(confirm(\"确定吗?\"))"+"{window.location.href=&#39;default.
asp
x&#39;;}"+"else{history.back();}"+"</script>");

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 Click Cancel to return to false, and execute history.back(); return to the original page

Related articles:

Click the return button to close in WeChat, Alipay, and Baidu Wallet Current page and window

JS code to automatically jump or close the current page after the specified time

Open a new window and close the current page without popping up Close prompt js code

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