javascript check cookies exist
get cookie in js
if cookie exists javascript
//You can call the function getCookie with the name of the cookie you want, then check to see if it is = null.
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
}
else
{
begin += 2;
var end = document.cookie.indexOf(";", begin);
if (end == -1) {
end = dc.length;
}
}
// because unescape has been deprecated, replaced with decodeURI
//return unescape(dc.substring(begin + prefix.length, end));
return decodeURI(dc.substring(begin + prefix.length, end));
}
function doSomething() {
var myCookie = getCookie("MyCookie");
if (myCookie == null) {
// do cookie doesn't exist stuff;
}
else {
// do cookie exists stuff
}
}
check cookie existence js
document.cookie = "hacker=anthony; SameSite=None; Secure";
function checkACookieExists() {
if (document.cookie.split(';').some((item) => item.trim().startsWith('hacker='))) {
const output = document.getElementById('a-cookie-existence')
output.textContent = '> The cookie "hacker" exists'
}
}
function clearOutputACookieExists() {
const output = document.getElementById('a-cookie-existence')
output.textContent = ''
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us