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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With