开发者

Javascript's redirection logic

开发者 https://www.devze.com 2023-03-21 17:44 出处:网络
So, if a have a Javascript function such as function doSomething() { alert(\"Starting...\"); window.location = \"http://www.example.com\";

So, if a have a Javascript function such as

function doSomething() {
     alert("Starting...");
     window.location = "http://www.example.com";
     alert("Completed.");
}

Why does the last line no开发者_运维技巧t work? I'm almost sure it's a security issue, but maybe I'm doing something wrong.

Thanks in advance.


Since you redirect the page, your browser start loading the new URL (http://www.example.com). The current page is unloaded, and the execution of the script stops.


Pretty sure it's just because you have left the page and the browser doesn't run javascript from pages not displayed.

To do this you'd probably need to use frames or else load the new page in an iFrame or equivalent.


I'm quite certain it's like expecting lines of code in java or c after a return command to execute. JS lives as long as the web page is open, and it dies as soon as you leave that page.


window.location = "..." causes a new request to be made and a new page to be loaded, so nothing after it will execute.

0

精彩评论

暂无评论...
验证码 换一张
取 消