如何在 Javascript 警报/确认框后将用户重定向到新网页

编程入门 行业动态 更新时间:2024-10-22 16:50:27
本文介绍了如何在 Javascript 警报/确认框后将用户重定向到新网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个客户希望在用户离开网站时弹出警告/确认框,然后根据他们的选择,他们要么留在页面上,要么转到新页面(如果它适用于所有浏览器).

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 警报/确认框后将用户重定向到新网页

本文发布于:2023-10-12 16:56:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485316.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:后将   警报   到新   重定向   网页

发布评论

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

>www.elefans.com

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