Answers for "if cookie exists javascript"

0

javascript check cookies exist

get cookie in js
Posted by: Guest on August-25-2021
-1

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
    }
}
Posted by: Guest on February-23-2021
-1

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 = ''
        }
Posted by: Guest on August-27-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language