473,508 Members | 2,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Jul 23 '05 #1
4 2281
> <ki********@hotmail.com> wrote in message
news:11**********************@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.
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 %>" ...>

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #2

cheers Grant - that worked great!!!

Jul 23 '05 #3
JRS: In article <11**********************@o13g2000cwo.googlegroups .com>
, dated Wed, 20 Apr 2005 10:01:12, seen in news:comp.lang.javascript,
ki********@hotmail.com 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)

--
© 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.
Jul 23 '05 #4
JRS: In article <aL****************@news2.mts.net>, dated Wed, 20 Apr
2005 17:58:30, seen in news:comp.lang.javascript, Grant Wagner
<gw*****@agricoreunited.com> posted :
<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.

--
© 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.
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
8814
by: Paul | last post by:
I link to a web site from an Excel spreadsheet. The page i link to is getCookie.asp which sets a cookie then returns back some html which opens a new window, to the same site but a different page...
2
2988
by: Syed Ghayas | last post by:
Hi, I've been having problem writing a cookie. Everything goes ok but when I supply the .Path property to "/" It just write the cookie when there is no cookie present, but when I try to update the...
7
2135
by: Christoph Pieper | last post by:
Hi, we've the following problem : We have an asp-application which sets the cookie on first login. The cookie will never be touched during user access. The user can work the whole day, but...
2
1600
by: marshalli | last post by:
Hi: I have a problem with writing cookie from Jacascript. My problem is that I have two server, one is A, and the other is B. (1) I call a aaa.html from A. In aaa.html : ... <iframe...
3
2027
by: Ben | last post by:
I'm having problems with cookies from asp.net to asp back to asp.net. It seems like I can set a cookie in asp.net fine, and alter it at will, as soon as asp touches it, asp.net won't have...
3
11096
by: Wysiwyg | last post by:
After a server created cookie is processed on the client I want it removed, cleared, or expired in the javascript block but have been unable to do this. If I set a cookie value in the server code...
5
5240
by: Miljana | last post by:
Hi, I have one problem with cookies in ASP.NET application. It seems that I can not retreive cookie from Request.Cookies collection. I put cookie in Response.Cookies collection, and after page...
6
2686
by: kelvlam | last post by:
Hello all, I'm still a bit new with JavaScript, and I hope the guru here can shed some light for me. It's regarding handling cookie and the case-sensitive nature of JavaScript itself. My...
5
2209
by: SJ | last post by:
Hi, In my website, i have a cookie that allows the user to remain logged in for upto 90days. So I'm setting the cookie expiration time to 90 days in the future from the time the user logs in....
29
2981
by: Jerim79 | last post by:
I did try to find the answer to this before posting, so this isn't a knee jerk reaction. What I am trying to accomplish is to have a script that opens a cookie, reads a value, and then use a...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7380
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
5050
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.