网站验证和跳转函数
这个网站包含两个主要的JavaScript函数:judgeWeb()
和 judegHttp()
。它们都用于验证用户的网络环境并在满足条件时进行页面跳转。
judgeWeb()
judgeWeb()
函数主要用于验证用户的网络环境是否为web端。它首先创建一个新的XMLHttpRequest对象,然后以POST方式向服务器发送请求。请求的URL是”/Cn/Verifyip/judgeWeb.html”。当请求的状态变为4(请求已完成)且状态码为200(请求成功)时,函数会解析服务器返回的JSON数据,并检查其中的’code’字段是否为1。如果’code’为1,表示用户网络环境为web端,则通过window.location = judObj.url;
实现页面跳转。
function judgeWeb() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "/Cn/Verifyip/judgeWeb.html", true);
xmlhttp.send();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var judObj = JSON.parse(xmlhttp.responseText);
console.log(judObj);
if (judObj.code == 1) {
window.location = judObj.url;
}
}
}
}
judegHttp()
judegHttp()
函数用于验证用户的网络环境是否为HTTPS,也就是说,它检查用户的网络是否安全。函数同样使用XMLHttpRequest对象,但是它获取的是当前页面的URL(通过var url = location.href;
)。然后,它将此URL作为POST请求的目标,并发送请求。与 judgeWeb()
类似,当请求完成且状态码为200时,解析返回的JSON数据并检查’code’字段是否为1。如果’code’为1,表示用户的网络环境是HTTPS,函数将不执行任何操作。否则,将显示一条消息通知用户他们的网络环境可能存在风险。
function judegHttp(){
var xmlhttp = new XMLHttpRequest();
var url = location.href;
xmlhttp.open("POST", "/Cn/Verifyip/judegHttp.html", true);
xmlhttp.send();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
var judObj = JSON.parse(xmlhttp.responseText);
console.log(judObj);
if (judObj.code !== 1) {
alert("您的网络环境可能存在风险!");
}
}
}
}