如何在 Javascript 警报/确认框后将用户重定向到新网页 [英] How to redirect a user to a new webpage after a Javascript Alert/confrim box

查看:35
本文介绍了如何在 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 = "http://www.google.com/";
}
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 警报/确认框后将用户重定向到新网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆