Is JavaScript location.href call is asynchronous?

有些话、适合烂在心里 提交于 2019-12-19 05:23:12

问题


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??");
}

If you see 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 .

So my question is , "Is location.href call is asynchronous in nature, if not then what is happening over here" ??

Because I thought the moment it will redirect the user to other page, the lines of code below it will never execute.

Any help/explanation is appreciated!!!

Thanks


回答1:


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 browsers speed or later synchronous input from the user (an alert in your case).



来源:https://stackoverflow.com/questions/37521172/is-javascript-location-href-call-is-asynchronous

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