topical media & game development

talk show tell print

professional-javascript-16-CookiesExample.htm / htm



  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  
  <html>
      <head>
          <title>Cookies Example</title>   
          <script type="text/javascript">
                  function setCookie(sName, sValue, oExpires, sPath, sDomain, bSecure) {
                      var sCookie = sName + "=" + encodeURIComponent(sValue);
                  
                      if (oExpires) {
                          sCookie += "; expires=" + oExpires.toGMTString();
                      }
                  
                      if (sPath) {
                          sCookie += "; path=" + sPath;
                      }
                  
                      if (sDomain) {
                          sCookie += "; domain=" + sDomain;
                      }
                  
                      if (bSecure) {
                          sCookie += "; secure";
                      }
                  
                      document.cookie = sCookie;
                  }
                                  
                  function getCookie(sName) {
                  
                      var sRE = "(?:; )?" + sName + "=([^;]*);?";
                      var oRE = new RegExp(sRE);
                      
                      if (oRE.test(document.cookie)) {
                          return decodeURIComponent(RegExp["$1"]);
                      } else {
                          return null;
                      }
                  
                  }                
  
                  function deleteCookie(sName, sPath, sDomain) {
                      var sCookie = sName + "=; expires=" + (new Date(0)).toGMTString();
                      if (sPath) {
                          sCookie += "; path=" + sPath;
                      }
                  
                      if (sDomain) {
                          sCookie += "; domain=" + sDomain;
                      }
                      
                      document.cookie = sCookie;
                  }
     
                          
          </script>           
      </head>
      <body>
          <script type="text/javascript">
              alert("Setting cookies...");
              setCookie("name", "Nicholas");
              setCookie("book", "Professional JavaScript");
              
              
              alert("The value of cookie 'name' is " + getCookie("name"));
              alert("The value of cookie 'book' is " + getCookie("book"));
          
              alert("Deleting cookies...");
              deleteCookie("name");
              deleteCookie("book");
              
              alert("The value of cookie 'name' is " + getCookie("name"));
              alert("The value of cookie 'book' is " + getCookie("book"));
              
          </script>
   
      </body>
  </html>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.