【问题标题】:local.Storage redirect if false如果为 false,则 local.Storage 重定向
【发布时间】:2013-04-14 16:47:37
【问题描述】:

我有一个需要像 cookie 一样进行验证的网站。但我想使用本地存储,因为它是一个移动概念。 我知道该项目是通过测试设置的,但是当它不正确时,我无法让页面重定向。

<script language="javascript">
 if (localStorage.getItem('itemVerified') = 'True') {
window.location = "success.html";
}
else {
alert("You are not able to enter this site!");
window.location = "error.html";
}
</script>

【问题讨论】:

  • 你有 = 而不是 == 这是一个 JS 错误,所以我看不出这会如何重定向。
  • 无论你看多少次你都看不到明显

标签: javascript html redirect local-storage


【解决方案1】:

可能有两个错误,第一个是您的比较字符串缺少一个 = 符号:

不正确:if (localStorage.getItem('itemVerified') = 'True') {

正确:if (localStorage.getItem('itemVerified') == 'True') {

第二种情况是,如果您要检查要从中加载文档的站点,您需要将操作添加到事件侦听器中,您可以执行以下操作。

    <script type="text/javascript">
     var verifyCookie = function(){

   if (localStorage.getItem('itemVerified') == 'True') {
    console.log("All right");
        window.location = "success.html";
   }
   else {
    alert("You are not able to enter this site!");
    window.location = "error.html";
   }
      };
     window.addEventListener ('DOMContentLoaded', verifyCookie, false);
    </script>

问候。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多