在onbeforeunload期间允许执行什么样的操作

时间:2015-04-21 03:07:14

标签: javascript

我决定在弹出确认退出对话框之前执行一些代码。

然而,onbeforeunload的行为令人费解。允许某些操作,并且不允许某些操作。

function dummy() {
    alert('123');            // Not allowed. Blocked alert('123') during beforeunload
    console.log('123');      // Allowed.
    navigator.vibrate(1000); // Doesn't work in my chrome mobile.
    return "do you exit";
}

window.onbeforeunload = function() {
    return dummy();
}

我在想,为什么会有这样的行为?有没有关于这种行为的文件?

在返回弹出字符串之前,是否有任何解决方法可以克服navigator.vibrate上的限制帖?

1 个答案:

答案 0 :(得分:4)

我认为你会发现Mozilla开发网络does have documentation on this sort of thing

引用:

  

自2011年5月25日起,HTML5规范声明在此事件期间可能会忽略对window.alert(),window.confirm()和window.prompt()方法的调用。

至于为什么,微薄的评论中很好地指出不应允许网站积极地阻止你离开页面。

navigator.vibrate是基于时间的功能。 "振动x毫秒"。当窗口卸载时,它不希望用户必须等待任何。您可以找到解决方法,但我怀疑它会被修补,因为这不是理想的功能。