Connecting Tech Pros Worldwide Forums | Help | Site Map

JS set cookie problem

kieran5405@hotmail.com
Guest
 
Posts: n/a
#1: Jul 23 '05

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="JavaScript">

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

cookie_name = "imageCookie";

if(document.cookie)
{
index = document.cookie.indexOf(cookie_name);

if (index != -1)
{

//refresh page
location.reload();

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

}
}
</SCRIPT>

</code>


Grant Wagner
Guest
 
Posts: n/a
#2: Jul 23 '05

re: JS set cookie problem


> <kieran5405@hotmail.com> wrote in message[color=blue]
> news:1114016472.079991.131460@o13g2000cwo.googlegr oups.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="yourimagename.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="yourimagename.jpg?<%= today %>" ...>

--
Grant Wagner <gwagner@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq


kieran5405@hotmail.com
Guest
 
Posts: n/a
#3: Jul 23 '05

re: JS set cookie problem



cheers Grant - that worked great!!!

Dr John Stockton
Guest
 
Posts: n/a
#4: Jul 23 '05

re: JS set cookie problem


JRS: In article <1114016472.079991.131460@o13g2000cwo.googlegroups .com>
, dated Wed, 20 Apr 2005 10:01:12, seen in news:comp.lang.javascript,
kieran5405@hotmail.com posted :[color=blue]
>
>var today = new Date()
>var expires = new Date()
>expires.setTime(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.setFullYear(expires.getFullYear() + 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.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Dr John Stockton
Guest
 
Posts: n/a
#5: Jul 23 '05

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@agricoreunited.com> posted :
[color=blue]
><script type="text/javascript">
>var today = new Date();
>document.write(
> '<img src="yourimagename.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
getTimezoneOffset 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.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Closed Thread