By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,381 Members | 1,626 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,381 IT Pros & Developers. It's quick & easy.

what's wrong with this...

P: n/a
I wrote some javascript code a couple of years ago to load a web page based
on the current date & time. I just modified it today to change the naming
format of the web pages and I can't get it to work at all. Even the old one
which was working (on Win98) is not working at all on my WinXP Pro. Has
some new requirement been introduced?

Can anyone see what's wrong with the following code. If it is 9am on July
12, it should load a web page named: Jul12-09.html, but it does nothing at
all.

<html>
<head>
<script language="JavaScript">
<!--
function lp() {
ms=new Array(11);
ms[0]="Jan";
ms[1]="Feb";
ms[2]="Mar";
ms[3]="Apr";
ms[4]="May";
ms[5]="Jun";
ms[6]="Jul";
ms[7]="Aug";
ms[8]="Sep";
ms[9]="Oct";
ms[10]="Nov";
ms[11]="Dec";
dt=new Date();
m=dt.getMonth();
d=dt.getDate();
d=(d<10)?"0"+d:d;
h=dt.getHours();
h=(h<10)?"0"+h:h;
location.href=ms[m]+d+"-"+h+".html";
}
//-->
</script>
</head>
<body onload="lp()"></body>
</html>

Thanks,
Ross Levis.
Jul 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Ross Levis wrote on 12 jul 2003 in comp.lang.javascript:
I wrote some javascript code a couple of years ago to load a web page
based on the current date & time. I just modified it today to change
the naming format of the web pages and I can't get it to work at all.
Even the old one which was working (on Win98) is not working at all on
my WinXP Pro. Has some new requirement been introduced?

Can anyone see what's wrong with the following code. If it is 9am on
July 12, it should load a web page named: Jul12-09.html, but it does
nothing at all.

<html>
<head>
<script language="JavaScript">
<!--
function lp() {
ms=new Array(11);
ms[0]="Jan";
ms[1]="Feb";
ms[2]="Mar";
ms[3]="Apr";
ms[4]="May";
ms[5]="Jun";
ms[6]="Jul";
ms[7]="Aug";
ms[8]="Sep";
ms[9]="Oct";
ms[10]="Nov";
ms[11]="Dec";
dt=new Date();
m=dt.getMonth();
d=dt.getDate();
d=(d<10)?"0"+d:d;
h=dt.getHours();
h=(h<10)?"0"+h:h;
location.href=ms[m]+d+"-"+h+".html";
}
//-->
</script>
</head>
<body onload="lp()"></body>
</html>


Please explain "not working", as this works:

<script language="JavaScript">
<!--
// function lp() {
ms=new Array(11);
ms[0]="Jan";
ms[1]="Feb";
ms[2]="Mar";
ms[3]="Apr";
ms[4]="May";
ms[5]="Jun";
ms[6]="Jul";
ms[7]="Aug";
ms[8]="Sep";
ms[9]="Oct";
ms[10]="Nov";
ms[11]="Dec";
dt=new Date();
m=dt.getMonth();
d=dt.getDate();
d=(d<10)?"0"+d:d;
h=dt.getHours();
h=(h<10)?"0"+h:h;
// location.href=ms[m]+d+"-"+h+".html";
alert(ms[m]+d+"-"+h+".html");
// }
//-->
</script>

Perhaps your relative location is not leading to a file.

Local time here still gives:

Jul12-08.html

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #2

P: n/a
DU wrote on 12 jul 2003 in comp.lang.javascript:
ms=new Array(11);
Your script is requesting to create an array for 11 items, not 12.


Good for you, I didn't see that!
I recommend explicitly declaring each local variables
with the var keyword.


why ?
DU wrote:
Your script is requesting to create an array for 11 items, not 12.

I wondered about that. I tried 12 but no difference.


Test it again in December ;-)

btw: cann't you do this with serverside redirecting ?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #3

P: n/a
In article <Fc*********************@news.xtra.co.nz>,
ro**@stationplaylist.dot.com says...
I wrote some javascript code a couple of years ago to load a web page based
on the current date & time. I just modified it today to change the naming
format of the web pages and I can't get it to work at all. Even the old one
which was working (on Win98) is not working at all on my WinXP Pro. Has
some new requirement been introduced?

Can anyone see what's wrong with the following code. If it is 9am on July
12, it should load a web page named: Jul12-09.html, but it does nothing at
all.


Works for me in IE6 and Moz1.something under XP Pro.

--
Hywel Never knowingly understood
http://hyweljenkins.co.uk/
http://hyweljenkins.co.uk/mfaq.php
Jul 20 '05 #4

P: n/a
DU
Ross Levis wrote:
DU wrote:
Your script is requesting to create an array for 11 items, not 12.

I wondered about that. I tried 12 but no difference.

I recommend explicitly declaring each local variables with the var


keyword.

OK, but that should not stop it working should it?


It's just good and sound coding techniques. Same thing with using a
doctype declaration and making sure that the markup is valid to begin
with. FYI, even MSDN kinda suggest this when script problems appear.
Ross.


When you say "not working", please be specific, give details, describe,
paste the javascript console error message(s) if any, define the
browsers and browser versions that do not work, etc... As far as I can
see, this should work and I tested the code (not the same as given) and
it worked in MSIE 6 for Windows (standards compliant rendering mode) and
with NS 7.1.

DU
--
Javascript and Browser bugs:
http://www10.brinkster.com/doctorunclear/

Jul 20 '05 #5

P: n/a
JRS: In article <Fc*********************@news.xtra.co.nz>, seen in
news:comp.lang.javascript, Ross Levis <ro**@stationplaylist.dot.com>
posted at Sat, 12 Jul 2003 17:41:56 :-
ms=new Array(11);
ms[0]="Jan";
ms[1]="Feb";
ms[2]="Mar";
ms[3]="Apr";
ms[4]="May";
ms[5]="Jun";
ms[6]="Jul";
ms[7]="Aug";
ms[8]="Sep";
ms[9]="Oct";
ms[10]="Nov";
ms[11]="Dec";


Your code fails appropriately in my system, complaining about the
absence of the page Jul12-18.html.

ms = ["Jan", "Feb", ..., "Dec"] // saves space, and counting.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #6

P: n/a
Dr John Stockton wrote on 12 jul 2003 in comp.lang.javascript:
JRS: In article <Fc*********************@news.xtra.co.nz>, seen in
news:comp.lang.javascript, Ross Levis <ro**@stationplaylist.dot.com>
posted at Sat, 12 Jul 2003 17:41:56 :-
ms=new Array(11);
ms[0]="Jan";
ms[1]="Feb";
ms[2]="Mar";
ms[3]="Apr";
ms[4]="May";
ms[5]="Jun";
ms[6]="Jul";
ms[7]="Aug";
ms[8]="Sep";
ms[9]="Oct";
ms[10]="Nov";
ms[11]="Dec";


Your code fails appropriately in my system, complaining about the
absence of the page Jul12-18.html.

ms = ["Jan", "Feb", ..., "Dec"] // saves space, and counting.


an alternate way saves on counting only:

ms=new Array(12);
// 12 !!
n = 0;
ms[n++]="Jan";
ms[n++]="Feb";
ms[n++]="Mar";
ms[n++]="Apr";
ms[n++]="May";
ms[n++]="Jun";
// these rows can easily be sorted alphabetically in an editor,
// not usefull in this months case of course.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #7

P: n/a
Lasse Reichstein Nielsen wrote on 13 jul 2003 in comp.lang.javascript:
"Evertjan." writes:
Test it again in December ;-)


It won't matter. Javascript arrays can have their lengths changed
dynamically, so when it executes
ms[11]="Dec";


I know that. It was partly ment as a joke. ";-)"

But also it is good practice to test such things out
for instance by temporariliy insertion:

.......
dt=new Date();
m=dt.getMonth();
m = 11; // this is the extra test line
.......
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.