如何使用 JavaScript 立即重定向

How to immediately redirect using JavaScript

如何让javascript立即停止脚本执行并切换到另一个页面。考虑一下:

<html>
<head>
    <script>
        function onload()
            {
                alert('before');
                window.location.href = 'http://google.com';
                alert('after redirect');
            }
    </script>
</head>
<body onload="onload();">
    Hello World!
</body>
</html>

第二个警报消息始终触发。

尝试 return 来自第一个函数的值,如果用户登录 return true。希望对您有所帮助。

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.
}

您调用 getUserInfo() 时没有条件句。所以这个函数将在页面加载时被调用,因为你没有对这个函数调用施加任何限制。因此,代码仍然是 运行.

也许这就是您代码段第一块的意图:

if(checkIfLoggedIn()) {
    getUserInfo();
}

这样,只有在 checkIfLoggedIn returns true 时才会调用 getUserInfo。按照你写的方式,getUserInfo 在页面加载时也是 运行。

如果您不想乱写代码,您可以打破所有规则并使用例外:

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();

抛出的异常将阻止所有其余代码的执行。

OK. It is ugly and breaks so many best practice rules. But it should work.

或者您可以在其他代码周围使用异常处理:

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();

我今天在寻找其他东西时遇到了答案:

即使 window.location.href 已设置并且 window 正在重定向,代码执行在大多数浏览器中继续:

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 1after redirect 2、&after redirect 3 即使页面被重定向,仍然会登录到控制台。但是,由于 init() 是顶级函数,我可以从函数中 return; 并停止代码继续执行..

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