Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
812 views
in Technique[技术] by (71.8m points)

window.location - Is JavaScript location.href call asynchronous?

function fun(){
    console.log("Hi");
    window.location.href="http://www.google.com";
    console.log("Hello, how are you");
    alert("I am good");
    fun1();
}

function fun1(){
    console.log("Whats up??");
}

In the above lines of code the location.href is getting called before console.log("Hello, how are you"), alert and fun1().

When I call the fun() it executes all the statements below location.href and then it redirects to https://www.google.com. Is the location.href call asynchronous in nature, and if not then what is happening?

I thought the moment it redirects the user to other page, the lines of code below it would never execute.

Any help/explanation is appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A browser will execute code after window.location.href = "http://google.com" until the browser goes to the next web address. As such, the number of lines that will be executed depends on some combination of the browser's speed or later synchronous input from the user (an alert in your case).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...