我有一个客户希望在用户离开网站时弹出警告/确认框,然后根据他们的选择,他们要么留在页面上,要么转到新页面(如果它适用于所有浏览器).
I have a client who wishes to have an alert/confirm box pop up when a user leaves the site, then based on what they choose, they will either stay on the page or go to a new page (would love if it would work in all browsers).
我折腾了一整天,得到了这段代码,但效果不太好.
I have been twiddling all day and have got this piece of code, but doesn't work too well.
<script> window.onbeforeonload = function exitLeave(){var answer = confirm("You have not filled out your questionnaire yet") if (answer){ window.location = "www.google/"; } else{ alert("Cancel it !") } } </script>任何帮助将不胜感激.
推荐答案正确的事件处理程序名称是 onbeforeunload(不是 onbeforeOnload).请参阅这个问题和我的回答(它指向 MDN,其中有一个关于如何实现您正在寻找的内容的小示例.)
The correct event handler name is onbeforeunload (not onbeforeOnload). See this SO question and my answer (it points to MDN, which has a small example of how to implement what you are looking for.)
更多推荐
如何在 Javascript 警报/确认框后将用户重定向到新网页
发布评论