Home > Web Front-end > HTML Tutorial > How to close the current page in html

How to close the current page in html

coldplay.xixi
Release: 2023-01-05 16:12:12
Original
12709 people have browsed it

html Methods to close the current page: 1. Customize the prompt to close; 2. When the user is about to leave the current page, the user clicks the close button onbeforeunload event in the browser's maximize-minimize close button.

How to close the current page in html

The operating environment of this tutorial: windows7 system, html5 version, DELL G3 computer.

htmlMethods to close the current page:

1. Customize the prompt to close

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

2. When you are about to leave the current page (refresh or close) When

The user clicks the close button in the maximize or minimize close button of the browser

The onbeforeunload event is triggered when the current page is about to be left (refreshed or closed).

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>html中</title>
</head>
<body onbeforeunload="return myFunction()">
<p>该实例演示了如何向 body 元素添加 "onbeforeunload" 事件。</p>
<p>关闭当前窗口,按下 F5 或点击以下链接触发 onbeforeunload 事件。</p>
<a href="http://www.runoob.com">点击调整到菜鸟教程</a>     
<script>
    function myFunction() {
        return "我在这写点东西...";
    }
</script>
</body>
</html>
Copy after login
rrree

Related learning recommendations: html video tutorial

The above is the detailed content of How to close the current page in html. 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