Does changing [removed] stop execution of javascript?

前端 未结 2 1861
失恋的感觉
失恋的感觉 2020-12-05 17:41

When writing server-side code you need to explicitly stop execution after sending a \"Location: ...\" header to the client or your code will continue to execute in the backg

相关标签:
2条回答
  • 2020-12-05 17:46

    Setting window.location does not implicitly stop JS execution. Take the following as an example:

    function locationTest() {
      window.location = 'http://www.google.com/';
      window.open('http://www.yahoo.com/');
    }
    
    locationTest();
    

    Try running that from Firebug/Web Inspector/etc. and you'll notice that the current window will load Google, but a new window will open with Yahoo as well.

    0 讨论(0)
  • 2020-12-05 17:52

    Does this immediately stop execution of the current script

    No, the remaining handler script will execute to the end before control returns to the browser and events start happening. When loading of the new page gets far enough for ‘navigation’ to occur, the beforeunload and unload events will fire, then the page and any script in it will become inactive.

    However, any further queued events and timeouts might not fire. For example if you navigate the page in a click handler of a form submit button and don't cancel the default action, it is possible (race condition) for the navigation to occur before the submit event queued by the default action of the click.

    0 讨论(0)
提交回复
热议问题