在Firefox中更改window.location.href以响应onunload事件

Fug*_*gue 1 javascript firefox javascript-events window.location

我使用window.location.href有一个奇怪的JavaScript问题,它显然只影响Firefox(我使用的是3.6).

通常window.location.href不是只读的,这在FF中完美运行:

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

但是,当我调用函数来响应onunload事件()时,这不能按预期工作:

function testThis() {
    alert ("1: " + window.location.href);
    window.location.href = "http://google.com/";
    alert ("2: " + window.location.href);
    return false;
}
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,警报都会在FF中显示页面的当前位置,而不进行更改.没有JavaScript错误,onunload事件成功调用该函数,因此问题似乎是编辑或替换window.location.href的值.

我尝试过使用window.location,document.location.href,甚至尝试更改window.location.search.事件(特别是onunload事件)是否可能导致window.location.href变为只读?

Gip*_*ing 5

是的,以防止恶意网页阻止用户离开.

  • 我非常感谢你:-) (2认同)