Home  >  Article  >  Web Front-end  >  How to close a web page using javascript

How to close a web page using javascript

coldplay.xixi
coldplay.xixiOriginal
2021-04-09 14:00:4612748browse

Javascript method to close a web page: 1. Close the window without any prompt; 2. Close the current page; 3. Customize the prompt to close; 4. The user clicks the [Close] button of the browser and a confirmation of closing pops up. dialog box.

How to close a web page using javascript

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.

Javascript method to close a web page:

1. JS code to close the window without any prompts

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

2. Close the current page (under IE With prompts)

<a href="javascript:window.opener=null;window.close();">关闭</a>

3. Custom prompts to close

<script language="javascript"> 
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()" 
/>

4. When the user clicks "Close" in the browser's "Maximize", "Minimize", and "Close" buttons When pressing the button, a closing confirmation dialog box also pops up.

The code is as follows:

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

Related free learning recommendations: javascript (video)

The above is the detailed content of How to close a web page using 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