javascript - 当 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 ;
}
解决方案
我的 js 代码浏览器中有后退按钮封锁,我删除了以下部分。
window.onpopstate = function () {
history.go(1);
};
推荐阅读
- php - 用于 php 7.0 的 PostgreSQL PDO 驱动程序
- openshift - 如何在命令提示符 Mac 中在线运行 openshift
- ios - pushViewController 时禁用导航栏透明度
- android - 牛津词典 Api 通过 EditText 搜索单词
- reactjs - Azure多个应用程序与反应,在浏览器中加载反应后无法访问其他应用程序
- excel - 具有动态范围的 VBA VLOOKUP
- php - Invision 电源板错误
- node.js - 使用 Jest 和 Mongoose 进行快速 API 测试
- asp.net-core - Autofac 和 Automapper - 已注册但未解析的配置文件
- java - 构造函数 Scanner(InputStream) 未定义