Display image using cookies?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mandy Holmes

    Display image using cookies?

    Hi all (Newbie alert!)

    I am a bit stuck. I want to set a cookie when a hyperlink on my website is
    clicked. This cookie will then be used to determing which of 5 images is
    displayed on that page and all the subsequent pages throughout the website.

    I don't know of the best way to do this as I am merely a graphic designer
    taking on a new challenge!

    If anyone has any suggestions it would be greatly appreciated.

    If you need any more information please ask

    Thanks in advance
    Mandy


  • kaeli

    #2
    Re: Display image using cookies?

    In article <1005bmhpsgj069 4@news.supernew s.com>, mandy@no-one-here.co.uk
    enlightened us with...[color=blue]
    > Hi all (Newbie alert!)
    >
    > I am a bit stuck. I want to set a cookie when a hyperlink on my website is
    > clicked. This cookie will then be used to determing which of 5 images is
    > displayed on that page and all the subsequent pages throughout the website.
    >
    > I don't know of the best way to do this as I am merely a graphic designer
    > taking on a new challenge!
    >[/color]

    That would depend on how willing you are to rewrite your hyperlinks. Not
    to mention the fact that not everyone enables cookies. Depending on them
    for functionality is generally VERY bad practice (of course, there are
    exceptions).
    That said...

    (note: old browsers do not support onClick of anchors, IIRC, and if you
    need to support them, would need to change to a "href=javascrip t:" type
    link)

    <a href="myLink.ht ml" onClick="setCoo kie(name,val,ti me);return true;">

    js code from my cookie file below.

    ------------

    /* This file contains cookie functions. */
    /* File Functions:
    1. setCookie - writes cookie
    2. getCookie - gets value of cookie
    3. removeCookie - deletes a cookie
    4. detectCookies - checks if cookies are enabled
    */

    function setCookie(cooki eName, cookieValue, expireDate)
    {
    /* Pass in three strings - the name of the cookie, the value, and the
    expire date.
    Pass in a "" empty string for expireDate to set a session cookie
    (no expires date).
    Pass in any other date for expire as a number of days to be added
    to today's date. */

    if (expireDate == "")
    {
    expires = "";
    }
    else
    {
    expires = new Date();
    expires.setDate (expires.getDat e() + expireDate);
    expires = expires.toGMTSt ring();
    }
    document.cookie = cookieName+"="+ cookieValue+";e xpires="+expire s;
    }

    function removeCookie (cookieName)
    {
    /* Pass in the name of the cookie as a string and it will be removed.
    */
    expires = Now();
    document.cookie = cookieName+"= ;expires="+expi res.toGMTString ();
    }

    function getCookie (cookieName)
    {
    cookieValue = ""
    if (document.cooki e.indexOf(cooki eName) == -1)
    {
    // there is no cookie by this name for this user
    return cookieValue;
    }
    else
    {
    // get the beginning index of the cookie by looking for the cookie
    name
    cookieStart = document.cookie .indexOf(cookie Name);
    // get the beginning index of the cookie value by looking for the
    equal sign after the name
    cookieValStart = (document.cooki e.indexOf("=", cookieStart) + 1);
    // get the end index of the cookie value by looking for the semi-
    colon after the value
    cookieValEnd = document.cookie .indexOf(";", cookieStart);
    // if no semi-colon, then use the whole length
    if (cookieValEnd == -1)
    {
    cookieValEnd = document.cookie .length
    }
    // use substring to get the text between the two indices and that
    is the value of the cookie
    cookieValue = document.cookie .substring(cook ieValStart,
    cookieValEnd);
    return cookieValue;
    }
    }

    function detectCookies()
    {
    /* function returns true if cookies are enables, false if not */
    setCookie("test ", "test", "");
    tmp = getCookie("test ")
    if (tmp != "test")
    {
    return false;
    }
    else
    {
    return true;
    }
    }

    ------------
    --
    --
    ~kaeli~
    Press any key to continue or any other key to quit



    Comment

    • Dr John Stockton

      #3
      Re: Display image using cookies?

      JRS: In article <MPG.1a6c890c27 7885b989ab3@nnt p.lucent.com>, seen in
      news:comp.lang. javascript, kaeli <tiny_one@NOSPA M.comcast.net> posted at
      Mon, 12 Jan 2004 10:36:35 :-
      [color=blue]
      >function setCookie(cooki eName, cookieValue, expireDate)
      > {
      > /* Pass in three strings - the name of the cookie, the value, and the
      >expire date.[/color]

      Expire interval would be a better term, as it is not a date; likewise
      elsewhere.

      [color=blue]
      > expires = new Date();[/color]
      // expires.setHour s(0,0,0,0) // could be useful[color=blue]
      > expires.setDate (expires.getDat e() + expireDate);
      > expires = expires.toGMTSt ring();[/color]



      [color=blue]
      >function detectCookies()
      > {
      > /* function returns true if cookies are enables, false if not */
      > setCookie("test ", "test", "");
      > tmp = getCookie("test ")
      > if (tmp != "test")
      > {
      > return false;
      > }
      > else
      > {
      > return true;
      > }
      > }
      >[/color]


      function detectCookies()
      { // should have had var tmp here I think.
      /* function returns true if cookies are enabled, false if not */
      setCookie("test ", "test", "");
      return getCookie("test ") == "test" // seems simpler
      }

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
      Web <URL:http://www.merlyn.demo n.co.uk/> - FAQish topics, acronyms, & links.
      Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
      Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

      Comment

      • kaeli

        #4
        Re: Display image using cookies?

        In article <0pBhGCCukwAAFw +8@merlyn.demon .co.uk>,
        spam@merlyn.dem on.co.uk enlightened us with...[color=blue]
        > JRS: In article <MPG.1a6c890c27 7885b989ab3@nnt p.lucent.com>, seen in
        > news:comp.lang. javascript, kaeli <tiny_one@NOSPA M.comcast.net> posted at
        > Mon, 12 Jan 2004 10:36:35 :-
        >[color=green]
        > >function setCookie(cooki eName, cookieValue, expireDate)
        > > {
        > > /* Pass in three strings - the name of the cookie, the value, and the
        > >expire date.[/color]
        >
        > Expire interval would be a better term, as it is not a date; likewise
        > elsewhere.
        >[/color]

        I hate when I change code then forget to change the comments. :)

        Thanks!

        --
        --
        ~kaeli~
        No one is listening until you make a mistake.



        Comment

        Working...