window.onbeforeunload不显示警告框

编程入门 行业动态 更新时间:2024-10-24 09:22:19
本文介绍了window.onbeforeunload不显示警告框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我只是想在离开页面之前显示一条消息,但我的代码不起作用:

I just want to show a message before leaving the page, but my code doesn't works:

window.onload=function(){ alert("Page with a digital clock"); setInterval(clock,1000); } window.onbeforeunload=function(){ alert("Are you sure to leave this page?"); }

onload alert工作正常,但第二个不工作..

The "onload alert" works fine, but the second is not working..

推荐答案

您无法在onbeforeunload中添加警报。大多数浏览器都会为您执行此操作,因此您无需处理它,您需要将确认消息返回给该方法:

You can't put an alert inside onbeforeunload. Most browsers will do this for you so you don't need to handle it, you need to return the confirm message to the method instead:

window.onbeforeunload=function(){ return "Are you sure to leave this page?"; }

以下是方法在MDN上。

当此事件返回非void值时,系统会提示用户确认页面卸载。在大多数浏览器中,事件的返回值显示在此对话框中

When this event returns a non-void value, the user is prompted to confirm the page unload. In most browsers, the return value of the event is displayed in this dialog

更多推荐

window.onbeforeunload不显示警告框

本文发布于:2023-11-27 21:02:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639591.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:window   onbeforeunload

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!