View Full Version : XHTML Help please
ManagerJosh
Sat 6th Dec '03, 8:47pm
Can anyone tell me why this script isn't XHTML compliant?
lightsup55
Sat 6th Dec '03, 11:18pm
If you are using <script> tags, you need to comment out the script.
Example:
<script type="text/javascript">
<!--
script to be executed
//-->
</script>
Why? If you use the & (ampersand) character, the validator will think that the entity "&" is not complete. To write an ampersand (&), use &.
With the "<" (less than), and ">" (greater than) characters, the validator will think you are starting an HTML tag, when you are only expressing math.
3 < 1 (3 is less than 1)
15 > 4 (15 is greater than 4)
To write "<" and ">" on the screen, you'd use "<" and ">"
< = <
> = >
"lt" being less than <
"gt" being greater than >
Simple to understand. Took me a while to figure out. :lol:
In JavaScript/VBscript, you can't use "&", "<", or ">" in replace of "&", "<", and ">". If you did, you'd get an error.
ONLY if you are writing these characters to the page (whether or not it being in plain HTML or in JavaScript), should you use entities.
What I mean by "writing it to the page" is actually displaying the character.
Example:
<b>my text</b>
produces bold text:
my text
while
<b>my text</b>
shows < and > on the page:
<b>my text</b>
The HTML BB Code tag shows what is in the HTML file while the QUOTE tag shows what will happen in the browser.
Much the same way with JavaScript:
<script type="text/javascript">
<!--
document.write('<b>my text<\/b>');
//-->
</script>
produces bold text:
my text
while
<script type="text/javascript">
<!--
document.write('\<\;b>\;my text\<\;\/b\>\;');
//-->
</script>
shows < and > on the page:
<b>my text</b>
P.S. In JavaScript, characters such as " ' & / and ; need to be escaped first before they can be written to the page:
<script type="text/javascript">
<!--
document.write('\" \' \& \/ and \;');
//-->
</script>
produces
" ' & / and ;
And to write a backslash "\", you'd use \\
Example:
<script type="text/javascript">
<!--
document.write('\\');
//-->
</script>
produces
\
And for those of you who don't know, \n writes a new line using JavaScript.
"Hello\neverybody"
Writes
"Hello
everybody"
If I did
\"\\\/\n\;\&\/\\\/\/\\\\'
it should show up as:
"\/
;&/\//\\'
All I did was add a backslash before each character (with the exception of \n which is a new line):
\"\\\/\n\;\&\/\\\/\/\\\\'
Hope that makes sense.
An entity reference can be found at www.w3schools.com (http://www.w3schools.com/) -> Learn HTML (http://www.w3schools.com/html/default.asp) -> HTML Entities (http://www.w3schools.com/html/html_entitiesref.asp)
And don't forget to add
type="text/javascript"
in the script tag! XHTML requires it.
Sorry if I've said something you already know. It is geared to those who don't know.
Full Script with HTML:
<script type="text/javascript">
<!--
function rnd(){return Math.random();}
function xRnd(){return (rnd()/10);}
function yRnd(){return (2 + 2*( .5 - rnd() ));}
var letitsnow = true;
var flakes = Array("/seasonthemes/snwflk.gif","/seasonthemes/snwflk2.gif","/seasonthemes/snwflk3.gif","/seasonthemes/snwflk4.gif");
var numflakes = 55;
var T = 5;
var dx, xPos, yPos, am, vx, vy, obj, i;
var winwidth = 800;
var winheight = 600;
function sizeIt()
{
window.winwidth = window.innerWidth?window.innerWidth:document.body. clientWidth;
window.winheight = window.innerHeight?window.innerHeight:document.bod y.clientHeight;
}
dx = new Array();
xPos = new Array();
yPos = new Array();
sway = new Array();
var swaymax = 20;
vx = new Array();
vy = new Array();
sizeIt();
document.write("<STYLE type=\"text/css\">\n.flk {position:absolute;top:-100;}<\/STYLE>");
for (i = 0; i < numflakes; i++)
{
var thisflake = "" + flakes[Math.floor(rnd()*flakes.length)];
dx[i] = 0;
xPos[i] = rnd()*(window.winwidth-30) +10;
yPos[i] = rnd()*window.winheight;
sway[i] = rnd()*swaymax;
vx[i] = xRnd();
vy[i] = yRnd();
document.write("<div id=\"f"+ i +"\" class=\"flk\"><img src=\"");
document.write(thisflake + "\" border=\"0\"><\/div>");
}
function snowMove(id,left,top)
{
obj = document.getElementById?document.getElementById(id ).style:
document.all?document.all[id].style:
document.layers?document.layers[id]:null;
if (obj)
{
obj.left=left;
obj.top=top;
}
}
function snowSwitch(s)
{
if ( s == "on" )
{
if ( window.letitsnow != true )
{
window.letitsnow = true;
doSnow();
}
} else if ( s == "off" )
{
window.letitsnow = false;
hideSnow();
}
}
function hideSnow()
{
for (i = 0; i < numflakes; ++ i) {
snowMove("f"+i,-100,-100);
}
}
function doSnow() {
if (letitsnow){
sizeIt();
delta = (window.pageYOffset!=null)?window.pageYOffset:docu ment.body.scrollTop;
for (i = 0; i < numflakes; ++ i) {
yPos[i] += vy[i];
if (yPos[i] > window.winheight+delta-50) {
xPos[i] = rnd()*(window.winwidth-sway[i]-30);
yPos[i] = delta;
vx[i] = xRnd();
vy[i] = yRnd();
}
dx[i] += vx[i];
snowMove("f"+i,xPos[i]+sway[i]*Math.cos(dx[i]),yPos[i]);
}
setTimeout("doSnow()", T);
}
}
function sunshine()
{
snowSwitch('off');return false;
}
window.onload=doSnow;
//-->
</script>
ManagerJosh
Sat 6th Dec '03, 11:21pm
okay, but in this case I'm doing
<script type="text/javascript" language="JavaScript1.2" src="../seasonthemes/snow2.js"></script>
ManagerJosh
Sat 6th Dec '03, 11:28pm
oh and one other thing, it doesn't seem to be obeying
function sizeIt()
{
window.winwidth = window.innerWidth?window.innerWidth:document.body. clientWidth;
window.winheight = window.innerHeight?window.innerHeight:document.bod y.clientHeight;
}
lightsup55
Sat 6th Dec '03, 11:36pm
okay, but in this case I'm doing
<script type="text/javascript" language="JavaScript1.2" src="../seasonthemes/snow2.js"></script>
I don't see the problem as long as the type="" attribute is there, all tag names in lower case, attribute names in lower case, attribute values quoted and that you aren't minimizing attributes (like <input disabled> which should be <input disabled="disabled" />.
Proably the thing to ask is what exact XHTML version were you validating that on?
I checked that on XHTML 1.0 Transitional with no errors.
http://www.htmlhelp.com/tools/validator/direct.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>test</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<div>
<script type="text/javascript" language="JavaScript1.2" src="../seasonthemes/snow2.js"></script>
</div>
</body>
</html>
ManagerJosh
Sun 7th Dec '03, 12:35am
how about the problem in Post #4?
lightsup55
Mon 8th Dec '03, 10:47pm
how about the problem in Post #4?
Like I had asked in the previous post, what program or site are you using to validate the file that contains the script tag used?
ManagerJosh
Tue 9th Dec '03, 4:14am
nothing really...
It was written by a fellow friend and the script works in HTML 4.01 Transitional but not XHTML Transitional :-/
noppid
Tue 9th Dec '03, 9:19am
Can anyone tell me why this script isn't XHTML compliant?
I added it to my test board in the headerinclude template with the script tags and it validates at http://validator.w3.org/ just fine.
<script type="text/javascript" >
<!--
function rnd(){return Math.random();}
function xRnd(){return (rnd()/10);}
function yRnd(){return (2 + 2*( .5 - rnd() ));}
var letitsnow = true;
var flakes = Array("/seasonthemes/snwflk.gif","/seasonthemes/snwflk2.gif","/seasonthemes/snwflk3.gif","/seasonthemes/snwflk4.gif");
var numflakes = 55;
var T = 5;
var dx, xPos, yPos, am, vx, vy, obj, i;
var winwidth = 800;
var winheight = 600;
function sizeIt()
{
window.winwidth = window.innerWidth?window.innerWidth:document.body. clientWidth;
window.winheight = window.innerHeight?window.innerHeight:document.bod y.clientHeight;
}
dx = new Array();
xPos = new Array();
yPos = new Array();
sway = new Array();
var swaymax = 20;
vx = new Array();
vy = new Array();
sizeIt();
document.write("<STYLE type=\"text/css\">\n.flk {position:absolute;top:-100;}<\/STYLE>");
for (i = 0; i < numflakes; i++)
{
var thisflake = "" + flakes[Math.floor(rnd()*flakes.length)];
dx[i] = 0;
xPos[i] = rnd()*(window.winwidth-30) +10;
yPos[i] = rnd()*window.winheight;
sway[i] = rnd()*swaymax;
vx[i] = xRnd();
vy[i] = yRnd();
document.write("<div id=\"f"+ i +"\" class=\"flk\"><img src=\"");
document.write(thisflake + "\" border=\"0\"><\/div>");
}
function snowMove(id,left,top)
{
obj = document.getElementById?document.getElementById(id ).style:
document.all?document.all[id].style:
document.layers?document.layers[id]:null;
if (obj)
{
obj.left=left;
obj.top=top;
}
}
function snowSwitch(s)
{
if ( s == "on" )
{
if ( window.letitsnow != true )
{
window.letitsnow = true;
doSnow();
}
} else if ( s == "off" )
{
window.letitsnow = false;
hideSnow();
}
}
function hideSnow()
{
for (i = 0; i < numflakes; ++ i) {
snowMove("f"+i,-100,-100);
}
}
function doSnow() {
if (letitsnow){
sizeIt();
delta = (window.pageYOffset!=null)?window.pageYOffset:docu ment.body.scrollTop;
for (i = 0; i < numflakes; ++ i) {
yPos[i] += vy[i];
if (yPos[i] > window.winheight+delta-50) {
xPos[i] = rnd()*(window.winwidth-sway[i]-30);
yPos[i] = delta;
vx[i] = xRnd();
vy[i] = yRnd();
}
dx[i] += vx[i];
snowMove("f"+i,xPos[i]+sway[i]*Math.cos(dx[i]),yPos[i]);
}
setTimeout("doSnow()", T);
}
}
function sunshine()
{
snowSwitch('off');return false;
}
window.onload=doSnow;
//-->
</script>
ManagerJosh
Wed 10th Dec '03, 3:31am
I'm referencing to the function SizeIt.
diades
Tue 6th Jan '04, 8:01am
Hi Guys
To be compliant for xhtml the script block on a page should be thus:
<script type="text/javascript">
//<![CDATA[
function foo(){
}
//]]>
</script>
and, if required, a style block thus:
<style type="text/css">
/*<![CDATA[*/
/*]]>*/
</style>
There is no language property as that has been deprecated. The CDATA section tells the parser to treat the enclosed script as data and ignore it.
vBulletin® v3.8.0 Release Candidate 1, Copyright ©2000-2008, Jelsoft Enterprises Ltd.