Forum Moderators: open

Message Too Old, No Replies

looking for "onLoad" alternative

to produce XHTML 1.0

         

sebbothebutcher

11:35 am on Aug 25, 2004 (gmt 0)

10+ Year Member



hi everyone!
i recently started changing my homepage, so it validates to correct XHTML 1.0. now everything's fine, except one attribute: i use
<body onLoad="clock()">
to start a running clock that is displayed using javascript, but according to the XHTML 1.0 Transitional standard it's not a valid attribute! is there a way to work around this problem? i've tried starting the clock like this:
<body>
<script language="javascript" type="text/javascript">
clock();
</script>

but the clock wasn't being displayed...
the code of the clock function itself looks like this:

<script language="javascript" type="text/javascript">
<!--
var digital = new Date()
function clock()
{
if (!document.all &&!document.getElementById) return;
var hours = digital.getHours();
var minutes = digital.getMinutes();
var seconds = digital.getSeconds();
digital.setSeconds( seconds+1 );
if (hours <= 9) hours = "0" + hours;
if (minutes <= 9) minutes = "0" + minutes;
if (seconds <= 9) seconds = "0" + seconds;
dispTime = hours + ":" + minutes + ":" + seconds;
if (document.getElementById)
document.getElementById("TIME").innerHTML = dispTime
else if (document.all)
TIME.innerHTML = dispTime;
setTimeout("clock()", 1000);
}
//-->
</script>

can anybody help me out?
thanks in advance

Span

11:50 am on Aug 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to use only lowercase for tags and attributes. So 'onload' will validate and 'onLoad' will not.
Also, the language attribute in your script tag is deprecated.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title></title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">//<![CDATA[
var digital = new Date()
function clock() {
if (!document.all &&!document.getElementById) return;
var hours = digital.getHours();
var minutes = digital.getMinutes();
var seconds = digital.getSeconds();
digital.setSeconds( seconds+1 );
if (hours <= 9) hours = "0" + hours;
if (minutes <= 9) minutes = "0" + minutes;
if (seconds <= 9) seconds = "0" + seconds;
dispTime = hours + ":" + minutes + ":" + seconds;
if (document.getElementById)
document.getElementById("TIME").innerHTML = dispTime
else if (document.all)
TIME.innerHTML = dispTime;
setTimeout("clock()", 1000);
}
//]]></script>
</head>
<body onload="clock();">
<div id="TIME">
</div>
</body>
</html>

py9jmas

12:03 pm on Aug 25, 2004 (gmt 0)

10+ Year Member



You're JavaScript program is commented out with <!-- and -->. With XHTML, the comment will be completely ignored. The rules for this are different between HTML and XHTML. See [hixie.ch...]

encyclo

12:07 pm on Aug 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



py9jmas, strictly speaking using HTML comments will silently remove the script contents in XHTML, but only when you are serving it with the mime type
application/xhtml+xml
- not when you are serving it with
text/html
.

However, you are correct in that you should remove the comments in XHTML to allow for any future change of mime type.

[edited by: encyclo at 12:08 pm (utc) on Aug. 25, 2004]

bird

12:08 pm on Aug 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You don't need to burden your HTML with this stuff.
After your function definition, add this line to the JavaScript:

window.onload=clock;

You can also move your JavaScript to an external file, and it will still work.

Bernard Marx

12:11 pm on Aug 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[ deleted: same as above ]

sebbothebutcher

12:14 am on Aug 26, 2004 (gmt 0)

10+ Year Member



thanks! i've learned something new...