How can I get javascript to immediately stop script execution and change to another page. Consider this:
<html>
<head>
<script>
function onload()
{
alert('before');
window.location.href = '';
alert('after redirect');
}
</script>
</head>
<body onload="onload();">
Hello World!
</body>
</html>
The second alert message always fires.
How can I get javascript to immediately stop script execution and change to another page. Consider this:
<html>
<head>
<script>
function onload()
{
alert('before');
window.location.href = 'http://google.';
alert('after redirect');
}
</script>
</head>
<body onload="onload();">
Hello World!
</body>
</html>
The second alert message always fires.
Share Improve this question edited Dec 4, 2019 at 13:59 Kyle B asked Mar 19, 2018 at 18:31 Kyle BKyle B 2,9091 gold badge24 silver badges43 bronze badges 2- well you call it onload so the whole page loads.... – epascarello Commented Mar 19, 2018 at 18:35
- Does this answer your question? How do I redirect to another webpage? – mhrabiee Commented Dec 4, 2019 at 11:11
4 Answers
Reset to default 2Try to return value from the first function and if a user logged in return true. I hope this will help you.
function init() {
if(checkIfLoggedIn()) {
getUserInfo(); // I would expect this to not run if the user was not logged in
}
}
function checkIfLoggedIn() {
if(loggedIn() === false) {
window.location.href = "/login.html";
} else {
return true;
}
}
function getUserInfo() {
var username = getUsername(); // throws exception if not logged in
var email = getEmail(); // throws exception if not logged in
// etc. etc.
}
You are calling getUserInfo()
with no conditionals around it. So this function will be called when the page loads, as you have placed no restrictions on this function call. Hence, code is still running.
Perhaps this is what is intended in the first block of your snippet:
if(checkIfLoggedIn()) {
getUserInfo();
}
This way, getUserInfo
will only be called if checkIfLoggedIn
returns true
. The way you have written it, getUserInfo
is also running when the page loads.
If you don't want to litter your code you can break all the rules and use an exception:
function init()
{
checkIfLoggedIn();
getUserInfo(); // I would expect this to not run if the user was not logged in
}
function checkIfLoggedIn()
{
if(loggedIn() === false) {
window.location.href = "/login.html";
throw new Error('Kill all code running after me');
}
}
function getUserInfo()
{
var username = getUsername(); // throws exception if not logged in
var email = getEmail(); // throws exception if not logged in
// etc. etc.
}
function loggedIn() {
return false;
}
init();
The thrown exception will prevent all of the rest of the code from executing.
OK. It is ugly and breaks so many best practice rules. But it should work.
Or you can use exception handling around the other code:
function init()
{
checkIfLoggedIn();
getUserInfo(); // I would expect this to not run if the user was not logged in
}
function checkIfLoggedIn()
{
if(loggedIn() === false)
window.location.href = "/login.html";
}
function getUserInfo()
{
try {
var username = getUsername();
var email = getEmail();
// etc. etc.
}
catch(ex) {
// Don't do anything here unless you really need to
}
}
function loggedIn() {
return false;
}
init();
I came across the answer today while I was looking for something else:
Even though window.location.href
has been set and the window is in the middle of redirecting, code execution continues in most browsers:
function onload()
{
console.log('before');
window.location.href = 'newpage.html';
console.log('after redirect 1');
console.log('after redirect 2');
console.log('after redirect 3');
}
after redirect 1
, after redirect 2
, & after redirect 3
still log to the console even though the page is being redirected. However, since init()
is a top level function, I can return;
from the function and stop code from continuing to execute..
<script>
function init()
{
if(checkIfLoggedIn() === false)
return; // <-- stops following code from executing and allows return to proceed
getUserInfo();
}
function checkIfLoggedIn()
{
var loggedIn = loggedIn();
if(loggedIn === false)
window.location.href = "/login.html";
return loggedIn;
}
...
</script>
</head>
<body onload="init()">
...