This is a script for showing time but it only shows in IE but not in firefox.Whats the problem?

<script language="javascript" type="text/javascript">


file://start display day/date code


DaysofWeek = new Array()
DaysofWeek[0]="Sunday"
DaysofWeek[1]="Monday"
DaysofWeek[2]="Tuesday"
DaysofWeek[3]="Wednesday"
DaysofWeek[4]="Thursday"
DaysofWeek[5]="Friday"
DaysofWeek[6]="Saturday"


Months = new Array()
Months[0]="January"
Months[1]="February"
Months[2]="March"
Months[3]="April"
Months[4]="May"
Months[5]="June"
Months[6]="July"
Months[7]="August"
Months[8]="September"
Months[9]="October"
Months[10]="November"
Months[11]="December"



function fixNumber(the_number){
if (the_number < 10){
the_number = "0" + the_number;
}
return the_number;
}


function fixPMHours(the_number){
if (the_number>12){
the_number = the_number - 12;
}
return the_number;
}



var dayVal;
var timeVal=new Date()
var m=timeVal.getMinutes()
var h=timeVal.getHours()
var fixed_hour = fixPMHours(h);
var da=timeVal.getDate()
var mo=timeVal.getMonth()
var year=timeVal.getYear()
var showDay=DaysofWeek[timeVal.getDay()]
var showMonth=Months[timeVal.getMonth()]
var fixed_minute = fixNumber(m);
var the_time = fixed_hour + ":" + fixed_minute;
var the_date = (showDay+", "+showMonth+" "+da+", "+year+"  ")


function showTime(){
var timeValue = the_date+" ";
timeValue +=(h >= 12) ? " " : " ";
document.write(timeValue);
}



file://end display day/date code
</script></font>
<font size="1" face="ms sans serif" color="#63C7DE">
<script language="javascript">
showTime();
</script>
</font></div>

Firefox requires the semicolons after the statements. IE lets you cheat.

You also have some HTML errors.

And you have not &-coded your < and > characters inside the script. The browser thinks they are an HTML tag.

It's better to have the main script in a separate file for the last reason.

> Firefox requires the semicolons after the statements. IE lets you cheat.
Bosh. Ecmascript doesn't require you to have semicolons at the end of statements. Automatic semicolon insertion takes place during the parsing phase. There are no browser exceptions to this rule.

> And you have not &-coded your < and > characters inside the script.
Bosh. If you do so, they would no longer be comparison operators.

> The browser thinks they are an HTML tag.
That depends on the DOCTYPE and is only true for XHTML documents.

<script language="javascript" type="text/javascript">
//start display day/date code
//file:

DaysofWeek = new Array()
DaysofWeek[0]="Sunday"
DaysofWeek[1]="Monday"
DaysofWeek[2]="Tuesday"
DaysofWeek[3]="Wednesday"
DaysofWeek[4]="Thursday"
DaysofWeek[5]="Friday"
DaysofWeek[6]="Saturday"

Months = new Array()
Months[0]="January"
Months[1]="February"
Months[2]="March"
Months[3]="April"
Months[4]="May"
Months[5]="June"
Months[6]="July"
Months[7]="August"
Months[8]="September"
Months[9]="October"
Months[10]="November"
Months[11]="December"


function fixNumber(the_number){
if (the_number < 10){
the_number = "0" + the_number;
}
return the_number;
}

function fixPMHours(the_number){
if (the_number>12){
the_number = the_number - 12;
}
return the_number;
}


var dayVal;
var timeVal=new Date()
var m=timeVal.getMinutes()
var h=timeVal.getHours()
var fixed_hour = fixPMHours(h);
var da=timeVal.getDate()
var mo=timeVal.getMonth()
var year=timeVal.getYear()
var showDay=DaysofWeek[timeVal.getDay()]
var showMonth=Months[timeVal.getMonth()]
var fixed_minute = fixNumber(m);
var the_time = fixed_hour + ":" + fixed_minute;
var the_date = (showDay+", "+showMonth+" "+da+", "+year+" ")

function showTime(){
var timeValue = the_date+" ";
timeValue +=(h >= 12) ? " " : " ";
document.write(timeValue);
}


//file://end display day/date code
</script></font>
<font size="1" face="ms sans serif" color="#63C7DE">
<script language="javascript">
showTime();
</script>
</font></div>

This works in both IE and FF. The problem was file: :twisted:

> Firefox requires the semicolons after the statements. IE lets you cheat.
Bosh. Ecmascript doesn't require you to have semicolons at the end of statements. Automatic semicolon insertion takes place during the parsing phase. There are no browser exceptions to this rule.

Then why do I get errors if I accidentally leave one out?

> And you have not &-coded your < and > characters inside the script.
Bosh. If you do so, they would no longer be comparison operators.

This is required for XHTML. He didn't specify a Doctype.

By the time the JS interpreter gets it, the &-codes are supposed to already be converted. But if the script is not embedded in the html, the &-codes must not be used.

Except for short calling scripts, it is always much better to have a separate JS file.

> The browser thinks they are an HTML tag.
That depends on the DOCTYPE and is only true for XHTML documents.

That's all I use for new pages now. Everyone should be doing this, to ensure future compatibility.

Maybe XHTML Doctypes require the semicolons too.

> Then why do I get errors if I accidentally leave one out?
Paste a code which supports your claim.

> This is required for XHTML.
No, it isn't. If you want scripts in your XHTML markup, enclose them in a CDATA section.

<script type="text/javascript">
// <![CDATA[
/* your javascript code here */
// ]]>
</script>

> That's all I use for new pages now. Everyone should be doing this, to ensure future
> compatibility.
IE is known to have serious problems with rendering XHTML, plus it is excessively strict. HTML 4 Strict works just fine. So unless you want to cut off the IE crowd or feel the pain, stay away from XHTML.

Paste a code which supports your claim.

I'll have to wait until it happens again.

IE is known to have serious problems with rendering XHTML, plus it is excessively strict. HTML 4 Strict works just fine. So unless you want to cut off the IE crowd or feel the pain, stay away from XHTML.

I test my pages before publishing. And I know how to fix the rendering problem.

> I'll have to wait until it happens again.
...which would be like, never?

> I test my pages before publishing.
Or so you think.

> And I know how to fix the rendering problem.
IE doesn't.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.