PDA

View Full Version : Variable gets stuck in IF stmt in Netscape but not MS-IE


laitueceleste
08-28-2002, 08:38 PM
I have a simple script that updates a link based on the month. The selection criteria looks at the month and year and picks a link out of the array and then prints it at the end of the script.

The problem is that I'm getting different results in Netscape and MS-IE. The value for i isn't passed in Netscape, but it is in MS-IE. I get different results in different browsers.

Here's the jist of the code:

var newDate = new Date()
var month = newDate.getMonth()
var year = newDate.getYear()
var txt = "Group Calendar"
var i = 3

var url = new Array
url[0] = "groupcal_aug2002.htm"
url[1] = "groupcal_sep2002.htm"
url[2] = "groupcal_oct2002.htm"
url[3] = "groupcal_nov2002.htm"
url[4] = "groupcal_dec2002.htm"

// August 2002
if ((month == 7) && (year == 2002))
{
var i = 0
}


// September 2002
if ((month == 8) && (year == 2002))
{
var i = 1
}


// October 2002
if ((month == 9) && (year == 2002))
{
var i = 2
}


// November 2002
if ((month == 10) && (year == 2002))
{
var i = 3
}


// December 2002
if ((month == 11) && (year == 2002))
{
var i = 4
}


// prints link to current calendar page
document.write('<a href=\"' + url[i] + '\" target=\"_top\">');
document.write(txt + '</a>');


It works fine in MS-IE. The link for August is the outcome. The value for i is passed to the end of the script. However, this doens't work in MS-IE. It gives me the link for November. So this means the value for i isn't being passed.

What is the problem? I have been investigating, but I don't know what the culprit could be.

beetle
08-28-2002, 09:39 PM
Where are they? You have no end-of-line semicolons, and if my calculations are correct you need about 16 or so. Put em in! It may fix your problem!

Couple more things In the document.write statements, it is unecessary to escape the double quotes
Better off defining your url array on one line (i.e., new Array('','','');)
It is unecessary to re-declare i within all your if() statements

jkd
08-28-2002, 10:37 PM
Originally posted by beetle
Where are they? You have no end-of-line semicolons, and if my calculations are correct you need about 16 or so. Put em in! It may fix your problem!

Semicolons are not required in ECMAScript to terminate lines, just to separate statements on one line.

beetle
08-28-2002, 10:46 PM
Is that right? huh. Well, I code in PHP too, and as I'm sure you are aware, it wigs out when you miss a semicolon, so I've learned to do the same :D

Pardon my ignorance, but what exactly is ECMAscript?

EDIT: Never mind just found out (http://www-106.ibm.com/developerworks/web/library/wa-emca.html?dwzone=web)

mordred
08-28-2002, 10:50 PM
The standardized specs for JavaScript implementations. Most recent version is ECMAScript 3, but version 4 is on the way with lots of additions. If I don't err, ECMAScript more or less corresponds to JavaScript1.5.

http://www.ecma.ch/ecma1/STAND/STANDARD.HTM#ECMA-262

nolachrymose
08-28-2002, 11:45 PM
var year = newDate.getYear()

In Mozilla, getYear() returns 102 for the current date, yet you are testing for the full year in all of your condition statements. I recommend you use getFullYear(), as it is always returns a four digit year.

Hope that helps!

Happy coding! :)

whammy
08-29-2002, 12:39 AM
I'll second that - it's most likely the problem.

laitueceleste
08-29-2002, 02:27 PM
Wow, guys, you all have a lot of collective experience and insight. Thank you so much for your help. I'm brand-new to javascript, just cracked a book a few days ago. Your feedback has been really helpful, and now I have a better sense of what to look out for.

Thanks so much and take care!

BrainJar
08-29-2002, 04:22 PM
I think you want

var year = newDate.getFullYear();

instead of .getYear() which returns a two-digit year in some implementations.