Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't we exit/die a script after redirect in Javascript?

We need to do exit; after redirect in php because exit; doesn't stop the rest of the code from running. But why we don't do something like that in javascript. I want to do something like

if(window.location.replace("http://something.com")){
  return;
}

or

window.location.replace("http://something.com");
throw "stop the execution.";

Do i need to do that ? If not why?

like image 650
user1906399 Avatar asked Sep 26 '14 06:09

user1906399


People also ask

How do you exit a JavaScript script?

Using return to exit a function in javascript Using return is the easiest way to exit a function. You can use return by itself or even return a value.

What is redirect in JavaScript?

Redirecting a URL in JavaScript is nothing but sending the user from a URL to another URL. In Javascript, window. location function is used to redirect to a URL.


1 Answers

Why we need to do exit(); after the redirection in php?

When you do

header( "Location: ./somepage.php" );

As php manual on header says, header() is used to send a raw HTTP header. So it doesnt stop the script from executing further. It sends the response header related to redirection back to browser and continues executing the code below that. So exit() would prevent doing that.

Do we need return; after the window.location calls?

The JS code says the browser to navigate to another url. So when the navigation is done. The browser navigates to new page and the code below the window.location will not be executed unlike the php behaviour explained above. So we dont require it.

like image 186
Mithun Satheesh Avatar answered Sep 26 '22 16:09

Mithun Satheesh