Reading Cookies : Cookie « Development « JavaScript Tutorial






var cookieName = document.cookie;

This statement creates the variable cookieName and assigns it the cookie property of the document object.

The cookie property itself returns a string containing all the cookies pertaining to the current document.

Cookies are interpreted in name/value pairs.

<html>
    <body>
    <script language="JavaScript">
    <!--
       var cookies = document.cookie;
       function readCookie(name) {
          var start = cookies.indexOf(name + "=");
          if (start == -1){
              alert("Cookie not found");
          }
          start = cookies.indexOf("=", start) + 1;
          var end = cookies.indexOf(";", start);
          if (end == -1){
              end = cookies.length;
          }
          var value = unescape(cookies.substring(start, end));
          if(value == null){
            alert("No cookie found");
          } else{
            alert("Cookie value is: " + value);
          }
       }
       readCookie("java2s.com");
    -->
    </script>
    </body>
    </html>








4.4.Cookie
4.4.1.JavaScript and Cookies
4.4.2.Reading Cookies
4.4.3.Writing Cookies
4.4.4.Create a cookie and read it back
4.4.5.Make cookie using Javascript
4.4.6.Delete all cookies
4.4.7.Read cookie value