Home > Web Front-end > JS Tutorial > body text

How to close a web page using javascript

coldplay.xixi
Release: 2023-01-04 09:35:40
Original
12802 people have browsed it

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>
Copy after login

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

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

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()" 
/>
Copy after login

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;">
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template