My html doesn't work. I wanna checking user are banned or not then i used:
<html>
<head style="background-color:#171745">
<style>
h1 {
color:white;
}
h3 {
color:white;
}
h5 {
color:gray;
}
p {
color:white;
}
</style>
</head>
<body>
<script>
function checkCookie() {
let ban = getCookie("banned");
if (ban != "1") {
alert("You are banned.");
} else {
window.location.replace("https://tuan-os.github.io/home")
alert("Welcome");
}
}
</script>
</body>
</html>
The user are banned will get a message say him are banned. The not banned user is redirect to tuan-os.github.io/home. But when i test it then the front my eyes is a blank and it no redirect like i think. And i wont the cookie is not detected by the verification....
First of all, you are not calling the function:
<script type="text/javascript">
function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(";").shift();
}
function checkCookie() {
let ban = getCookie("banned");
if (ban != "1") {
alert("You are banned.");
} else {
window.location.replace("https://tuan-os.github.io/home");
alert("Welcome");
}
}
checkCookie(); // You should always call the function after creating
</script>
Secondly, there is no getCookie function by default. You should create one using the script like below if you want to get a cookie stored in the browser:
function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(";").shift();
}