捕获/覆盖Javascript重定向

Tre*_*vor 3 html javascript redirect dom href

我将如何覆盖/捕获JavaScript重定向?

例如:

window.location.href="http://example.com";
Run Code Online (Sandbox Code Playgroud)

值“ http://example.com”将被捕获并可能被更改。

Dar*_*rov 5

抱歉,您可以捕获此事件,但不能更改目标URL。如果用户要取消重定向,则只能提示用户。为此,您可以订阅before unload事件

window.onbeforeunload = function (e) {
    var e = e || window.event;

    // For IE and Firefox prior to version 4
    if (e) {
        e.returnValue = 'Are you sure you want to leave the site?';
    }

    // For Safari
    return 'Are you sure you want to leave the site?';
};
Run Code Online (Sandbox Code Playgroud)

  • @Boldewyn:Returnign`false`不起作用。您只能返回一个字符串。 (2认同)