Changing window.location.href in Firefox in response to an onunload event

我与影子孤独终老i 提交于 2019-12-11 10:05:44

问题


I have a strange JavaScript problem using window.location.href, which apparently only affects Firefox (I'm using 3.6).

Normally window.location.href would not be read-only, and this works perfectly in FF:

window.location.href = "http://google.com/";

However, when I call a function in response to an onunload event (), this doesn't work as expected:

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

In both cases, the alert displays the current location of the page in FF, without making the change. There are no JavaScript errors, and the onunload event successfully calls the function, so the problem appears to be editing or replacing the value of window.location.href.

I've tried using window.location, document.location.href, even tried changing window.location.search. Is it possible that an event, specifically an onunload event, causes window.location.href to become read-only?


回答1:


Yes, to prevent malicious webpages from blocking the user from leaving.




回答2:


For the record, firefox seems to use document.location in lieu of document.location.href.



来源:https://stackoverflow.com/questions/2357625/changing-window-location-href-in-firefox-in-response-to-an-onunload-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!