JSNewbie wrote:
I am new to javascript. I used the code below in Netscape 7.0 and in
IE 6.0. The Javascript portion does not render in Netscape , but it
does show in IE. Can someone explain why? Any helpful insight would
be appreciated.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html xmlins="http://www.w3.org/1999/xhtml">
<head>
<title>Java Script Test</title>
</head>
<body bgcolor="#FFFFF">
<h1> What is your problem
</h1>
<script language="javascript"
type="text javascript">
<!-- Hide script from old browsers
document.write("<h1> Today is </h1>")
// End hiding script from old broswers -->
</script>
</body>
</html>
The DOCTYPE is incomplete, and it should have "html" instead of "HTML".
The namespace property is spelled "xmlns", not "xmlins".
Forcing the body background color only is bad practice, because you
don't know what the user's default are. Either set all the colors or
leave them alone.
The "language" property of the "script" element is obsolete.
The MIME type for JavaScript is "text/javascript", not "text javascript".
Using <!-- ... --> is obsolete.
document.write does not work with XHTML (and isn't supposed to). Use the
DOM model.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Java Script Test</title>
</head>
<body>
<h1> What is your problem</h1>
<script type="text/javascript">
var body = document.getElementsByTagName("body")[0];
var h1 = document.createElement("h1");
h1.appendChild (document.createTextNode (" Today is "));
body.appendChild (h1);
</script>
</body>
</html>
--
John W. Kennedy
"Compact is becoming contract,
Man only earns and pays."
-- Charles Williams. "Bors to Elayne: On the King's Coins"