Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2012
    Location
    Bokaro Steel City, India
    Posts
    21
    Thanks
    14
    Thanked 1 Time in 1 Post

    Question JavaScript Cookies!

    Hello everyone!

    I am having really tough time learning JavaScript Cookies!

    Can somebody explain me how to use them?

    "Please do not provide any external links! It is a request!"

    Any type of basic lesson/advice/suggestion will be quite helpful to me.

    Thanks!


  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Why do you not want external links? You will find plenty of good references if you Google. There is no point in re-inventing the wheel - there are plenty of cookie scripts out there. And you do not tell us what your difficulty is. Do you understand what a cookie is and what it does? Have you tried using the search feature of this forum?

    Here is a basic cookie code. It consists of four functions to - SET the cookie, GET (retrieve) the cookie, DISPLAY the cookie and DELETE the cookie.

    Code:
    <html>
    <head>
    
    <script type = "text/javascript">
    
    var expDate = new Date();
    expDate.setTime(expDate.getTime()+365*24*60*60*1000); // one year ahead
    
    function setCookie(isName,isValue,dExpires) {
    document.cookie = isName + "=" + isValue + ";expires=" + dExpires.toGMTString() + "path=/";
    alert (document.cookie);  // for testing
    }
    
    function getCookie(isName){
    cookieStr = document.cookie;
    startSlice = cookieStr.indexOf(isName+"=");
    if (startSlice == -1) {return false}
    endSlice = cookieStr.indexOf(";",startSlice+1)
    if (endSlice == -1){endSlice = cookieStr.length}
    isData = cookieStr.substring(startSlice,endSlice)
    isValue = isData.substring(isData.indexOf("=")+1,isData.length);
    return isValue;
    }
    
    function dispCookie(isName) {
    nValue = getCookie(isName);
    alert(nValue);
    }
    
    function deleteCookie(isName){
    if (getCookie(isName)){
    document.cookie = isName + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
    }
    
    </script>
    
    </head>
    
    <body>
    <input type = "button" value = 'Set 1st Cookie' onclick = "setCookie('anyName','Hello',expDate)">
    &nbsp;&nbsp;<input type = "button" value = 'Set 2nd Cookie' onclick = "setCookie('someName','Goodbye',expDate)">
    <br><br>
    <input type = "button" value='Delete 1st Cookie' onclick="deleteCookie('anyName')">
    &nbsp;&nbsp;<input type = "button" value = 'Delete 2nd Cookie' onclick = "deleteCookie('someName')">
    <br><br>
    <input type = "button" value = 'Read 1st Cookie' onclick = "dispCookie('anyName')">
    &nbsp;&nbsp;<input type = "button" value = 'Read 2nd Cookie' onclick="dispCookie('someName')">
    </body>
    
    </html>
    "In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)
    Last edited by Philip M; 02-27-2012 at 03:20 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    TheAliveWinner (02-27-2012)

  • #3
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,391
    Thanks
    11
    Thanked 568 Times in 561 Posts
    Short introduction
    1. There is a "cookie" property of the "document" object.
    2. If you read it, you will get all the cookies set for the current page.
    Code:
    var allCookies = document.cookie;
    3. If you set it, you will have to provide a string with a specific format "key=value". If you want to provide an expiration date, you'll have to use "key=value; expires=DATE". A previous cookie with the same key will be overwritten. Setting key=null will delete the cookie
    Code:
    var tomorrow = new Date();
    tomorrow.setTime(tomorrow.getTime() + 24*60*60*1000);
    document.cookie="mycookie=beenthere; expires=" + tomorrow.toGMTString();
    4. If you want to get a specific cookie, you'll have to split "document.cookie" at each ";" semicolon. Each of the elements can then be splitted at the "=" equal sign to get key and value pairs

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    726
    Thanks
    35
    Thanked 132 Times in 123 Posts
    Someday soon, cookies will return to the server and clients will use local storage.

    All modern browsers, including IE8, already support both persistent localStorage and temporary sessionStorage.

    If you are learning, learn to write to a cookie only when you must, and use storage when you can.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Quote Originally Posted by mrhoo View Post
    All modern browsers, including IE8, already support both persistent localStorage and temporary sessionStorage.
    Yes indeed. But cookies are fine for many purposes and will continue to be supported. The main issue here is the low 4KB limit.

    An important issue is that the data stored in local storage is much more public than that stored in cookies, which can be limited to a certain path within a domain. Even picking a hard-to-guess key won't provide any privacy because the Storage object provides a way to enumerate them. localStorage presents significantly higher security risks than cookies.

    My advice would be to continue use cookies unless there is a reason for using localStorage, such as the need for greater storage capacity than a cookie can provide. Offhand I cannot think of another reason in most contexts.
    Last edited by Philip M; 02-27-2012 at 05:38 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •