what's wrong with this...

R

Ross Levis

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.
 
E

Evertjan.

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
 
E

Evertjan.

DU wrote on 12 jul 2003 in comp.lang.javascript:
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 ?
I wondered about that. I tried 12 but no difference.

Test it again in December ;-)

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

Hywel Jenkins

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

DU

Ross said:
I wondered about that. I tried 12 but no difference.



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.

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
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
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.
 
E

Evertjan.

Dr John Stockton wrote on 12 jul 2003 in comp.lang.javascript:
JRS: In article <[email protected]>, seen in
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.
 
E

Evertjan.

Lasse Reichstein Nielsen wrote on 13 jul 2003 in comp.lang.javascript:
Evertjan. said:
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
.......
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top