javascript - 在 cookie 检查后,页面继续加载 JS 上的 XML 请求
问题描述
我是 JS 的新手,所以任何帮助将不胜感激。我在页面加载视频时创建了一个 XML 请求以检查 cookie,然后用我网站中的外部 HTML 文件替换 div 元素“tmx”。但是,在将 XML 请求注入“#tmx”之后,页面会在获取 XML 请求后继续重新加载。我注意到的另一件事是,当我在未选中复选框的情况下按下文档上的接受 tnc 按钮时,整个页面正在完全重新加载。我认为这可能是返回值可能是错误的。
function setCookie(cname, cvalue, exdays) {
if(document.getElementById('agree').checked){
const d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
let expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
function checkCookieOnLoad() {
if(getCookie("clicklink") === "yes") {
var elem = document.getElementById("tmx");
elem.parentNode.removeChild(elem);
// var elem2 = document.getElementById("ooo");
// elem2.parentNode.removeChild(elem2);
var video = document.createElement('video');
video.type = "video/mp4";
video.src = ".//vid1.mp4";
video.autoplay = true;
video.muted = true;
video.id = "vdd1"
document.body.appendChild(video);
video.addEventListener("timeupdate", function(){
// current time is given in seconds
if(this.currentTime >= 4.8) {
// pause the playback
this.pause();
this.remove();
location.replace("https://www.test.com/index.html");
}
});
return true;
} else {
const xhr = new XMLHttpRequest();
const contianer = document.getElementById('tmx');
xhr.onload = function() {
if (this.status ===200){
contianer.innerHTML = xhr.responseText;
}
}
xhr.open('get', 'tnc.html');
xhr.send();
}
}
解决方案
setTimeout(function() {
function setCookie(cname, cvalue, exdays) {
if (document.getElementById('agree').checked) {
const d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
let expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
}
return "";
}
function checkCookieOnLoad() {
if (getCookie("clicklink") === "yes") {
var elem = document.getElementById("tmx");
elem.parentNode.removeChild(elem);
// var elem2 = document.getElementById("ooo");
// elem2.parentNode.removeChild(elem2);
var video = document.createElement('video');
video.type = "video/mp4";
video.src = ".//vid1.mp4";
video.autoplay = true;
video.muted = true;
video.id = "vdd1"
document.body.appendChild(video);
video.addEventListener("timeupdate", function() {
// current time is given in seconds
if (this.currentTime >= 4.8) {
// pause the playback
this.pause();
this.remove();
location.replace("https://www.test.com/index.html");
}
});
return true;
} else {
const xhr = new XMLHttpRequest();
const contianer = document.getElementById('tmx');
xhr.onload = function() {
if (this.status === 200) {
contianer.innerHTML = xhr.responseText;
}
}
xhr.open('get', 'tnc.html');
xhr.send();
}
}
}, 5000);
/*This code will run after 5 seconds after the loading of HTML CSS and your JS files. Try it. Hope it helps you!!*/
推荐阅读
- java - 如何在 jdbc 驱动程序加载中实现快速失败策略?
- api - 如何在 Twitter 上搜索特定的表情符号(Twitter Premium API)?
- python - 如何在python中获取轴坐标
- powershell - 在 powershell 中删除关闭按钮 $host.ui.PromptForCredential
- linux - 没有包管理器时如何安装软件
- java - 您的 SQL 语法中存在错误,以便在“join-date DATE,credit DOUBLE(15,0) zerofill,PRIMARY KEY(id))”附近使用正确的语法
- python - Python numpy'数组索引太多'
- python - 如何在 tkinter 上制作秒表/计时器
- flutter - 在 Flutter 中映射流
- python - pygame窗口上奇怪的黑色矩形