Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
807 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 Answer

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...