Mega Code Archive

 
Categories / JavaScript Tutorial / Development
 

Reading Cookies

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("rntsoft.com");     -->     </script>     </body>     </html>