0

I'm using jQuery BlockUI plugin to display some nice ("Please wait...") message every time user changes URL, clicks link, is redirected, goes somewhere etc.:

$(window).on('beforeunload', function(){$.blockUI();});

Now, I'd like to enhance this message with a Cancel button, that would prevent such redirect and let user stay on current page. This leads me to a question: Is there any way in Javascript to stop URL change process, that is in progress? Or any other method/solution, that I could bind in this Cancel button's click event?

Am I trying to do something stupid or useless?

4

1 回答 1

2

您可以尝试e.preventDefault()return false在 beforeunload 位中尝试,但出于安全原因,浏览器在技术上不支持您想要做的事情。想象一个无法关闭的窗口的含义......

最好的办法是使用内置语法。返回一个字符串会beforeunload弹出一个对话框,询问您是否确定要退出,该字符串作为问题和选项“离开页面”或“留在页面上”。

例子

$(window).on('beforeunload', function(){
    return "Do you really want to leave this page?";
});

更新

在进一步阅读后发现主流浏览器支持返回字符串语法,但 Firefox 不支持。Firefox 将简单地将您的字符串替换为“您确定要退出此页面吗?”。或类似的东西。

TLDR:您可以在所有浏览器中使用上述返回字符串语法,但您的自定义语句不会在 Firefox 中显示 :)

于 2014-06-15T21:02:38.223 回答