首页 > 解决方案 > 当 cookie 为空或不存在时打开 html 或 url

问题描述

我正在尝试以各种方式打开另一个 html 文件或 url,但它们都不起作用。

location.href = 'new_url';
location.replace('https://www.javascripttutorial.net/');
location.assign('https://www.javascripttutorial.net/');

所以,我有一个函数,当 body 加载它检查 cookies 'username' 和 'password' 是否为空或丢失。这会触发警报,然后在我单击警报弹出窗口上的“确定”按钮后,它应该重定向但它没有。

弹出警报,因此代码可以正常工作,但是当我按下蓝色 OK 按钮时,它不会重定向

请帮忙?

我遇到问题的部分 JS 代码

    function check_cookie(){
     if ((getCookie('username') == null) || (getCookie('password') == null) || (getCookie('username') == "") || (getCookie('password') == "")){
        alert('Session Expired!');
        location.assign("https://stackoverflow.com/questions/503093/how-do-i-redirect-to-another-webpage");
        return ;
  }

标签: javascripthtmlredirectcookies

解决方案


我的 js 代码浏览器中有后退按钮封锁,我删除了以下部分。

window.onpopstate = function () { 
    history.go(1); 
}; 

推荐阅读