最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does setting window.location.href not stop script execution - Stack Overflow

programmeradmin8浏览0评论

The following code changes the location to www.bing regardless of wether redirect is 1 or any other number. If redirect is 1, it logs "is redirecting" and then redirects to www.bing. My best guess is that when href is set a change-event is triggered, but it takes some ticks before it executes. Meanwhilethe first line of code after is still executed. Or? do anyone what happens?

if (redirect == 1) {
    console.log("is redirecting");
    window.location.href = "";
}

window.location.href = "";

The following code changes the location to www.bing. regardless of wether redirect is 1 or any other number. If redirect is 1, it logs "is redirecting" and then redirects to www.bing.. My best guess is that when href is set a change-event is triggered, but it takes some ticks before it executes. Meanwhilethe first line of code after is still executed. Or? do anyone what happens?

if (redirect == 1) {
    console.log("is redirecting");
    window.location.href = "http://www.google.";
}

window.location.href = "http://www.bing.";
Share Improve this question asked Apr 4, 2016 at 9:05 BildsoeBildsoe 1,3506 gold badges32 silver badges45 bronze badges 3
  • 1 Interesting! MDN says that "Whenever a new value is assigned to the location object, a document will be loaded using the URL as if location.assign() had been called". Do you get the same behaviour with .assign()? – Emil Vikström Commented Apr 4, 2016 at 9:12
  • 2 @DanFromGermany - I did indeed solve it with an else, but it bugs me, that I don't know why it continues – Bildsoe Commented Apr 4, 2016 at 9:17
  • 1 @EmilVikström - Same thing happens with location.assign. – Bildsoe Commented Apr 4, 2016 at 9:17
Add a ment  | 

3 Answers 3

Reset to default 9

You are setting a property, not calling a method, the difference is important:

window.location.href = "something";

Javascript works asyncronous and event based, that means the browser doesn't check for the window location changing right when you set the property, but some unpredictable time later.

Sometimes you want exit-events to be triggered or fired when the location changes.

When you pare this to the HTTP location header, the behaviour is the same. Sometimes you want for example PHP scripts to continue even you send the location change header.

I would say the behaviour is undefined.

When you set the expected url to window.location.href, the current page will actually be in the dying state. And the result of the code written after that is simply unpredictable.

That's because everything after the if is executed as well, but it looks like there's a difference between Chrome and FF (haven't tested in IE).

Example code:

var redirect = 1;

if (redirect == 1) {
    console.log("is redirecting");
    document.location.assign('https://developer.mozilla/');
}

alert('go');
document.location.assign('https://www.bing./');

In Chrome 'go' is alerted, and you're redirected to bing.. In Firefox, 'go' is alerted, but you're redirected to MDN.

I think, the best thing to do is to wrap the other stuff in an else.

发布评论

评论列表(0)

  1. 暂无评论