mariannnn
mariannnn

Reputation: 287

Javascript's redirection logic

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 not work? I'm almost sure it's a security issue, but maybe I'm doing something wrong.

Thanks in advance.

Upvotes: 0

Views: 156

Answers (4)

Mark D
Mark D

Reputation: 5648

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.

Upvotes: 0

Petar Ivanov
Petar Ivanov

Reputation: 93030

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

Upvotes: 0

user684934
user684934

Reputation:

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.

Upvotes: 0

slaphappy
slaphappy

Reputation: 6999

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.

Upvotes: 1

Related Questions