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