Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在执行IIFE javascript函数之前,HTML呈现一秒钟?_Javascript_Html - Fatal编程技术网

在执行IIFE javascript函数之前,HTML呈现一秒钟?

在执行IIFE javascript函数之前,HTML呈现一秒钟?,javascript,html,Javascript,Html,我有一个非常基本的身份验证功能,如果用户未登录,我将使用该功能将用户重定向到login.html。我遇到的问题是在一瞬间,index.html在重定向实际发生之前呈现。有什么方法可以防止我现有的代码出现这种情况吗?我读了一篇类似的文章,创建了一个“空白页”并基于此重定向,但有没有其他方法可以避免这种情况,并以不同的方式进行呢 app.js const unauthPaths = ['/login.html', '/register.html']; (function () { const

我有一个非常基本的身份验证功能,如果用户未登录,我将使用该功能将用户重定向到login.html。我遇到的问题是在一瞬间,index.html在重定向实际发生之前呈现。有什么方法可以防止我现有的代码出现这种情况吗?我读了一篇类似的文章,创建了一个“空白页”并基于此重定向,但有没有其他方法可以避免这种情况,并以不同的方式进行呢

app.js

const unauthPaths = ['/login.html', '/register.html'];

(function () {

  const currPath = window.location.pathname;
  const isLoggedIn = localStorage.getItem('authToken');

  if (unauthPaths.includes(currPath)) {
    return;
  }

  if (!isLoggedIn) {
    window.location.href = '/login.html';
  }

})();
index.html

<html lang="en">
<head>
  <meta charset="utf-8">

  <link rel="stylesheet" href="styles.css">
</head>

<body>
  <h1>Home</h1>

<script src="./js/app.js"></script>
</body>
</html>

有两种方法:

  • 在脚本看到需要重定向时立即调用。调用
    window.stop
    后分配给
    window.location.href
  • 将脚本移动到HTML的顶部,而不是底部,以便它立即运行,而不是在加载文档的其他部分后运行
  • (如果您不喜欢其他方法:)使用JavaScript呈现页面的其余部分,而不是将其放入源HTML中
  • 不过,在前端执行此操作的一般方法并不那么优雅。最好将此逻辑移到服务器,通过cookie发送authtoken,并在需要时让服务器执行重定向

    • 有两种方法:

      • 在脚本看到需要重定向时立即调用。调用
        window.stop
        后分配给
        window.location.href
      • 将脚本移动到HTML的顶部,而不是底部,以便它立即运行,而不是在加载文档的其他部分后运行
      • (如果您不喜欢其他方法:)使用JavaScript呈现页面的其余部分,而不是将其放入源HTML中
      • 不过,在前端执行此操作的一般方法并不那么优雅。最好将此逻辑移到服务器,通过cookie发送authtoken,并在需要时让服务器执行重定向