Javascript help please

Discussion in 'JavaScript' started by SearchBliss, Jun 23, 2009.

  1. #1
    Can anyone help me with this?

    I'm looking for a javascript that will add a cookie when the page loads, then read the cookies value whenever the user returns to that page, adding the coockie value to a hidden text field. I can do this with ASP, but the client's page is an HTML page that cannot be changed to an ASP page. Any help is greatly appreciated! Thank you.
     
    SearchBliss, Jun 23, 2009 IP
  2. franklyn

    franklyn Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    tinyurl[dot]com/cfsuy6
     
    franklyn, Jun 23, 2009 IP
  3. JavaScriptBank.com

    JavaScriptBank.com Peon

    Messages:
    141
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    JavaScriptBank.com, Jun 23, 2009 IP
  4. Unni krishnan

    Unni krishnan Peon

    Messages:
    237
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    0
    #4
    Here is the code for your functionality:

    <html>
    <head>
    
    <script language="javascript">
    
    function createCookie(name,value,days) // fn. for creating cookies
    {
    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=/";
    }
    
    function readCookie(name)
    {
    var flag = 0;
    
    var dcmntCookie = document.cookie.split(';'); // getting all cookies in the document and splitting them and storing in an array
    
    for(var i=0;i < dcmntCookie.length;i++) // looping through the cookies array
    {
    var ck = dcmntCookie[i];
    while (ck.charAt(0)==' ') // loop for removing space at the begining
    {
    ck = ck.substring(1,ck.length);
    }
    if(ck)
    {
    cparts = ck.split('='); // splitting the cookie. eg: mycookie="some message"
    
    if (cparts[0]==name)
    {
    
    flag=1;
    document.getElementById('hiddenField').value=cparts[1]; //storing value of cookie to hidden field
    //alert(document.getElementById('hiddenField').value);
    break;
    }
    }
    
    }
    
    if(!flag) // if no such cookie exist, then create a new one
    createCookie(name,"cookie 4 the day",1);
    }
    
    </script>
    
    </head>
    
    <body onLoad="readCookie('MyCookie')">
    
    <input type="hidden" id="hiddenField" name="hiddenField"/>
    
    </body>
    
    </html>
    Code (markup):
    Hope this helps.
     
    Unni krishnan, Jun 24, 2009 IP
    SearchBliss likes this.
  5. SearchBliss

    SearchBliss Well-Known Member

    Messages:
    1,899
    Likes Received:
    70
    Best Answers:
    2
    Trophy Points:
    195
    Digital Goods:
    1
    #5
    I'll take a look thanks! The issue was never the cookie itself, it was populating the hidden textfield with the cookie value. Thanks again.
     
    SearchBliss, Jun 24, 2009 IP
  6. SearchBliss

    SearchBliss Well-Known Member

    Messages:
    1,899
    Likes Received:
    70
    Best Answers:
    2
    Trophy Points:
    195
    Digital Goods:
    1
    #6
    It worked perfectly, rep has been sent, thanks!
     
    SearchBliss, Jun 24, 2009 IP