I want to make an index.html page that redirects to home.html if the JavaScript _phstup cookie is set to false and if it's set to true, redirect to /videos/home.html. When I run my code below, I get redirected to /videos/home.html whether or not the cookie is set to true, false, or anything else. When I just console.log(getcookie("_phstup")); I get false when it's set to false, and true when it's set to true. So my cookie is okay. But my if this then that isn't. So I tried doing an if this then that so that if the cookie is set to true, it will log "not working!" in the console, and otherwise log "working!" since the cookie is set to false by default, and sure enough, it logged "not working!" Here is an image.
Here is my code:
function getcookie(name = "") {
let cookies = document.cookie;
let cookiestore = {};
cookies = cookies.split(";");
if (cookies[0] == "" && cookies[0][0] == undefined) {
return undefined;
}
cookies.forEach(function (cookie) {
cookie = cookie.split(/=(.+)/);
if (cookie[0].substr(0, 1) == " ") {
cookie[0] = cookie[0].substr(1);
}
cookiestore[cookie[0]] = cookie[1];
});
return name !== "" ? cookiestore[name] : cookiestore;
}
if (getcookie("_phstup") != "true") {
window.location("https://<domain>/videos/home");
} else {
console.log("kinda working!");
}
Aucun commentaire:
Enregistrer un commentaire