JS set cookie problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kieran5405@hotmail.com

    JS set cookie problem


    Hi,

    I have an Intranet page that has an image that changes each day, but
    the image is caching and not updating until the user manually does a
    page refresh. I want the page to refresh itself but i dont want to use
    a timed refresh such as every 5 mins etc. I want it so when the user
    comes in in the morning and opens the page it will have the updated
    image immed.

    I am thinking that JavaScript code to check for a cookie and if it is
    not there perform a page refresh. Then write the cookie to the user's
    computer that has a life span of 8 hours. That way the cookie should
    expire by the next morning as most users leave at 5pm.

    I am using the following code but cant seem to get it to work - or even
    write the cookie.

    Any help much appreciated.... .....

    <code>
    <SCRIPT LANGUAGE="JavaS cript">

    var today = new Date()
    var expires = new Date()
    expires.setTime (today.getTime( ­) + 60*60*24*365)

    cookie_name = "imageCooki e";

    if(document.coo kie)
    {
    index = document.cookie .indexOf(cookie _name);

    if (index != -1)
    {

    //refresh page
    location.reload ();

    //create new cookie
    document.cookie =cookie_name +"; expires=" + expire.toGMTStr ing()

    }
    }
    </SCRIPT>

    </code>

  • Grant Wagner

    #2
    Re: JS set cookie problem

    > <kieran5405@hot mail.com> wrote in message[color=blue]
    > news:1114016472 .079991.131460@ o13g2000cwo.goo glegroups.com.. .
    >
    > Hi,
    >
    > I have an Intranet page that has an image that changes each day, but
    > the image is caching and not updating until the user manually does a
    > page refresh. I want the page to refresh itself but i dont want to
    > use
    > a timed refresh such as every 5 mins etc. I want it so when the user
    > comes in in the morning and opens the page it will have the updated
    > image immed.[/color]

    I'm not sure what the desired goal is, to refresh the page, or to ensure
    the image is "fresh", it seems like you want both, but I'll stick to
    solving the image "freshness" issue, because it seems to me that is what
    the post is about.
    [color=blue]
    > I am thinking that JavaScript code to check for a cookie and if it is
    > not there perform a page refresh. Then write the cookie to the user's
    > computer that has a life span of 8 hours. That way the cookie should
    > expire by the next morning as most users leave at 5pm.[/color]

    I assume the image is changed on the server but named the same? If so,
    use the following:

    <script type="text/javascript">
    var today = new Date();
    document.write(
    '<img src="yourimagen ame.jpg?' +
    today.getYear() +
    today.getMonth( ) +
    today.getDate() +
    '" ...>'
    );
    </script>

    It doesn't produce a human readable date, but what it does produce is a
    unique value for every day of every year, ensuring that when the page is
    reloaded (by whatever means) the URL to the image is different for each
    day. Most user agents (Web browsers) will see this different URL as a
    completely new resource and insist on loading it from server because
    yesterday the user agent cached: "yourimagename. jpg?105319" but today
    the user agent is requesting: "yourimagename. jpg?105320", which is does
    not have a cached copy of.

    If you have server-side processing available (Perl, PHP, ASP, JSP,
    ColdFusion, etc), it would be even better to do the unique image URL
    there, avoiding any dependancy on client-side JavaScript. Example in
    server-side JavaScript:

    <%
    var today = new Date();
    today = today.getYear() + today.getMonth( ) + today.getDate() ;
    %>

    <img src="yourimagen ame.jpg?<%= today %>" ...>

    --
    Grant Wagner <gwagner@agrico reunited.com>
    comp.lang.javas cript FAQ - http://jibbering.com/faq


    Comment

    • kieran5405@hotmail.com

      #3
      Re: JS set cookie problem


      cheers Grant - that worked great!!!

      Comment

      • Dr John Stockton

        #4
        Re: JS set cookie problem

        JRS: In article <1114016472.079 991.131460@o13g 2000cwo.googleg roups.com>
        , dated Wed, 20 Apr 2005 10:01:12, seen in news:comp.lang. javascript,
        kieran5405@hotm ail.com posted :[color=blue]
        >
        >var today = new Date()
        >var expires = new Date()
        >expires.setTim e(today.getTime (­) + 60*60*24*365)[/color]

        The - is inappropriate; remove it.
        Methods setTime, getTime use milliseconds, not seconds.
        the number of days in a year is often not 365.

        var expires = new Date()
        expires.setFull Year(expires.ge tFullYear() + 1)

        Or, for an approximate year,

        var expires = new Date(+new Date()+31e9)

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
        <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

        Comment

        • Dr John Stockton

          #5
          Re: JS set cookie problem

          JRS: In article <aLw9e.285$3d3. 1401@news2.mts. net>, dated Wed, 20 Apr
          2005 17:58:30, seen in news:comp.lang. javascript, Grant Wagner
          <gwagner@agrico reunited.com> posted :
          [color=blue]
          ><script type="text/javascript">
          >var today = new Date();
          >document.write (
          > '<img src="yourimagen ame.jpg?' +
          > today.getYear() +
          > today.getMonth( ) +
          > today.getDate() +
          > '" ...>'
          >);
          ></script>
          >
          >It doesn't produce a human readable date, but what it does produce is a
          >unique value for every day of every year,[/color]

          It does not discriminate between Feb 11 & Dec 1 ... Feb 19 & Dec 9. One
          might do better with the order M Y D.
          I have heard of a system in which getYear() repeats 01..99, 00 every
          century.

          But it should suffice for the purpose.

          Math.floor(new Date()/864e5) will change once per 24 hours, though if
          getTimezoneOffs et is not included it may change at an inconvenient time
          of the local day.

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
          Web <URL:http://www.merlyn.demo n.co.uk/> - w. FAQish topics, links, acronyms
          PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see 00index.htm
          Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

          Comment

          Working...