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()">
...