学堂 学堂 学堂公众号手机端

HTML页面关闭的提醒与取消怎样实现,方法是什么

lewis 1年前 (2024-03-11) 阅读数 2 #技术
今天这篇我们来学习和了解“HTML页面关闭的提醒与取消怎样实现,方法是什么”,下文的讲解详细,步骤过程清晰,对大家进一步学习和理解“HTML页面关闭的提醒与取消怎样实现,方法是什么”有一定的帮助。有这方面学习需要的朋友就继续往下看吧!

1、不带任何提示关闭窗口的js代码

1 <input type="button" name="close" value="关闭" onclick="window.close();" />

2、自定义提示关闭

 1 <script language="javascript">
 2 // 这个脚本是 ie6和ie7 通用的脚本
 3 function custom_close(){
 4     if(confirm("您确定要关闭本页吗?")){
 5         window.opener=null;
 6         window.open('','_self');
 7         window.close();
 8     }
 9     else{
10     }
11 }
12 </script>
13 <input id="btnClose" type="button" value="关闭本页" onClick="custom_close()" />

3、在即将离开当前页面(刷新或关闭)时

用户点浏览器的最大化最小化关闭按钮中的关闭按钮时
onbeforeunload 事件在即将离开当前页面(刷新或关闭)时触发。

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

现在大家对于HTML页面关闭的提醒与取消怎样实现,方法是什么的内容应该都有一定的认识了吧,希望这篇能对大家有所帮助。最后,想要了解更多,欢迎关注博信,博信将为大家推送更多相关的文章。

版权声明

本文仅代表作者观点,不代表博信信息网立场。

热门