JS set cookie problem

K

kieran5405

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>
 
G

Grant Wagner

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'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.
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 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 %>" ...>
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Wed, 20 Apr 2005 10:01:12, seen in (e-mail address removed) posted :
var today = new Date()
var expires = new Date()
expires.setTime(today.getTime(­) + 60*60*24*365)

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)
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Wed, 20 Apr
2005 17:58:30, seen in Grant Wagner
<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,

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top