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 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2010
    Location
    Milkyway Galaxy
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Writing and Reading Cookies with Javascript

    I've looked on several websites and several threads here regarding writing and reading cookies with javascript, and I just can't seem to get it to work. All I want is a simple text box with 2 buttons: Save and Recall. Text entered into box is saved to a cookie when "Save" is pressed, and at a later date when "Recall" is pressed the saved text will show up in the box.

    Can you help please?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,242
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    Cookies have been covered very many times in this forum. Did you try using the Search feature?

    Code:
    <input type = "text" id = "txtInput">
    <input type = "button" value = "Save" onclick = "createCookie('mycookie', document.getElementById('txtInput').value, 10)">
    <input type = "button" value = "Recall" onclick = "readCookie('mycookie')">
    
    <script type = "text/javascript">
    
    function createCookie(name,value,days) {
    if (days) {
    var date = new Date();
    date.setTime(date.getTime() + (days*24*60*60*1000));
    var expires = "; expires = "+ date.toGMTString();
    }
    else{var expires = ""}
    document.cookie = name + "=" + value + expires + "; path=/";      // available throughout the directory
    }
    
    function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
    var cb = ca[i];
    while (cb.charAt(0)==' ') {cb = cb.substring(1,cb.length)}
    if (cb.indexOf(nameEQ) == 0) {
    retval = cb.substring(nameEQ.length,cb.length);
    document.getElementById("txtInput").value =  retval;
    return retval;
    }
    }
    return null;  // no display if cookie not found
    }
    
    function eraseCookie(name) {
    createCookie(name,"",-1);  // set cookie to expire on previous day
    }
    
    //eraseCookie("mycookie", "", -1);  // if required for testing
    
    </script>
    10 means 10 days, the number of days persistence for the cookie before it expires. Adjust the number to suit your requirements.


    Once a powerful king went to see an old hermit who lived in a bird's nest in the top of a tree, and asked "What is the most important Buddhist teaching?"
    The hermit answered, "Do no evil, do only good. Purify your heart." The king had expected to hear a very long explanation. He protested, "But even a five-year old child can understand that!" "Yes," replied the wise sage, "but even an 80-year-old man cannot do it."
    Last edited by Philip M; 11-20-2010 at 09:53 PM.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,103
    Thanks
    80
    Thanked 4,554 Times in 4,518 Posts
    Look in this thread:
    http://www.codingforums.com/showthread.php?t=209724

    Shows saving and recalling the value of a button, so it's really the same thing. Just use the value of your text field instead.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New Coder
    Join Date
    Jan 2010
    Location
    Milkyway Galaxy
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thank you both! I'll try with your help to accomplish what I want. If not, I guess I'll be back to ask for further assistance

    And yes, as I mentioned in the OP I used the search feature for this problem, but I didn't understand the several threads I looked at.


  •  

    Posting Permissions

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