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
    }
}

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
JS chack if cookie exists js check if a cookie exists check if cookie exist js check if a cookie exists javascript jsp check if cookie exists how to check cookie exists in javascript if cookie exist js check if cookie exists before set javascript javascript does cookie exist javascript update cookie if exists js cookie doesnt exists on document.cookie test if cookie exists javascript check if a cookie exists javascript check if something in cookie check to see if cookie exists check if request.cookie exists how to check if cookie exists js redirect if cookie exists check cookie exist javascript check if cookie exists nodejs how to check if cookie name exists how to check if cookie exists in javascript trough it's index javascript check if specific cookie exists check if cookie exists node js if cookie exists jquery test if cookie exists check if cookie exists jquery check if cookie exists before displaying check if cookie exist check exist cookie nodejs check cookie exists javascript how to check if a cookie exists in javascript javascript check if cookie name exists check if cookie exists javascript check if cookie exists in javascript and display value in html check if cookie exists js javascript see if cookie exists javascript if cookie exists if cookie does not exist javascript tchek if cookie exist js js check if cookie exists how to check if client have cookie exist check if a cookie exists check if cookie exists how to check if cookie exists in node js at backend how to check if cookie exists in node js how to check if cookie exists in javascript javascript check cookie exists see if cookie exists on server javascript check if cookie exist javascript javascript check if cookie exists javascript check if httponly cookie exists javascript check if cookie exists nodejs js check cookie exists if cookie exists javascript
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source