Setting cookies in JavaScript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rjoseph
    New Member
    • Sep 2007
    • 36

    Setting cookies in JavaScript?

    Hi Guys

    I think I have a really easy one for you.

    I am currently using the following script to store information in cookies:

    -------------SCRIPT--------------
    <%
    response.cookie s("prefs")("mak e")="Aston Martin"
    response.cookie s("prefs")("mod el")="Vanquis h"
    response.cookie s("prefs")("pri ce")="£40000"
    For Each cookie in Response.Cookie s
    Response.Cookie s(cookie).Expir es = now()+365
    Next
    %>
    -------------END-------------

    This works just fine in my ASP pages but now I need to incorporate the same method into some xml pages and therefore believe I need to use Javascript. I have spent hours reading numerous (how to set cookies in Javascript) articles and have not found a simple example that matches what I want to achieve above.

    Can anyone please put me out of my misery and provide me with a simple piece of code that I can use in my xml page to achieve the above?

    If so, then that would be great.

    I look forward to hearing from you

    Rod from the UK
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Originally posted by rjoseph
    Hi Guys

    I think I have a really easy one for you.

    I am currently using the following script to store information in cookies:

    -------------SCRIPT--------------
    <%
    response.cookie s("prefs")("mak e")="Aston Martin"
    response.cookie s("prefs")("mod el")="Vanquis h"
    response.cookie s("prefs")("pri ce")="£40000"
    For Each cookie in Response.Cookie s
    Response.Cookie s(cookie).Expir es = now()+365
    Next
    %>
    -------------END-------------

    This works just fine in my ASP pages but now I need to incorporate the same method into some xml pages and therefore believe I need to use Javascript. I have spent hours reading numerous (how to set cookies in Javascript) articles and have not found a simple example that matches what I want to achieve above.

    Can anyone please put me out of my misery and provide me with a simple piece of code that I can use in my xml page to achieve the above?

    If so, then that would be great.

    I look forward to hearing from you

    Rod from the UK

    Look this is a Java Forum.
    Java!=JavaScrip t :-)
    So post the code in JavaScript forum.
    And have a look at this code ......
    [code=javascript]
    function createCookie(na me,value,days) {
    if (days) {
    var date = new Date();
    date.setTime(da te.getTime()+(d ays*24*60*60*10 00));
    var expires = "; expires="+date. toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+ expires+"; path=/";
    }

    function readCookie(name ) {
    var nameEQ = name + "=";
    var ca = document.cookie .split(';');
    for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c .length);
    if (c.indexOf(name EQ) == 0) return c.substring(nam eEQ.length,c.le ngth);
    }
    return null;
    }

    function eraseCookie(nam e) {
    createCookie(na me,"",-1);
    }
    [/code]
    Good Luck with your try :-)

    Kind regards,
    Dmjpro.

    Comment

    • rjoseph
      New Member
      • Sep 2007
      • 36

      #3
      Sorry

      I posted my problem the wrong forum.

      Best regards

      Rod from the UK

      Comment

      Working...