shr*_*nsh 11 javascript window.location
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??");
}
Run Code Online (Sandbox Code Playgroud)
如果你看到上面的代码行在location.hrefconsole.log之前被调用("你好,你好吗"),alert和fun1().
当我调用fun()它时执行下面的所有语句location.href然后重定向到https://www.google.com.
所以我的问题是," location.href呼叫本质上是异步的,如果不是,那么这里发生了什么"?
因为我认为它将用户重定向到其他页面的那一刻,它下面的代码行将永远不会执行.
任何帮助/解释表示赞赏!
谢谢
ŹV *_*V - 11
浏览器将执行代码,直到window.location.href = "http://google.com"浏览器转到下一个网址。因此,将执行的行数取决于浏览器速度或用户稍后的同步输入(alert在您的情况下)的某种组合。