javascript - 检查Javascript后重定向不起作用
问题描述
从下面的脚本中,我请求 index2.html 的状态。
我的http://test1.info/portal/#portal
无法访问(Chrome)或显示(IE)。由于 URL 为“404”或未到达,因此不会将其重定向到index3.html
function website() {
var req = new XMLHttpRequest();
req.open('GET', 'http://mytest.info/index2.html', true);
alert(req.status);
req.send();
if (req.status != "200"
|| "404") {
window.location.href = "http://test1.info/portal/#portal";
} else {
alert("123");
alert(req.status);
window.location.href = "http://test.example.info/index3.html";
}
}
<body onload="website()" >
我在哪里做错了?
解决方案
您的代码中有一些拼写错误:
)
你的body
标签中有一个- 你还没有关闭你的
body
标签 - 在
if
你使用的条件下or
,它在 JS 中不存在(它是||
),但在你的情况下,你的逻辑应该如下:req.status != "200" && req.status != "404"
解决这些问题后,您的代码应该可以工作:
function website() {
var req = new XMLHttpRequest();
req.open('GET', 'http://mytest.info/index2.html', true);
alert(req.status);
req.send();
if (req.status != "200" && req.status != "404") {
window.location.href = "http://test1.info/portal/#portal";
} else {
alert("123");
alert(req.status);
window.location.href = "http://test.example.info/index3.html";
}
}
<body onload="website()"></body>
推荐阅读
- angularjs - 是否有一种标准方法可以将额外的 html 插入到一次性案例的指令模板中?
- fullcalendar - 如何在 Fullcalendar v4 中使用上一个/下一个时段在时间线视图中设置自定义可见范围?
- c# - C# WPF MVVM | 添加未显示在列表中的新对象
- node.js - 如何检查我的 pod 身份是否适用于 node.js 应用程序?
- typescript - 未找到连接“默认”
- python-3.x - 如何在 cython 中编写带有嵌套括号的#define 表达式?
- c++ - QT 实时摄像头显示因内存不足而崩溃
- php - 将 whereIn 与 Laravel 中单独查询的值一起使用
- javascript - Y 滚动事件监听器 useEffect
- python-3.x - 对字典中的数据运行许多函数