error in formatting a FOR line

M

Mechphisto

I've a JS script I'm using:

<script type="text/javascript">
function showS(ind){
var sel =
document.getElementById('container').getElementsByTagName('div');
for(var i=0;i<sel.length;i++){
sel.style.display=(i==ind)?'block':'none';
}
}
onload = function(){showS(0)};//
document.form_event_sel.sel_event_type.selectedIndex = 0;
</script>

But when I W3C validate my page, I get the error:

start tag was here
for(var i=0;i<sel.length;i++){

and it underlines the less-than symbol in the FOR line.
And then this error:

character ";" not allowed in attribute specification list
for(var i=0;i<sel.length;i++){

and it underlines the semicolon after "length".
I think this is the root of all my other validation errors that go:

error parsing attribute name
for(var i=0;i<sel.length;i++){

underlying the "h" in the word "length" and

Opening and ending tag mismatch: body line 76 and form
</form>
and all the other supposed tag mismatches. I think all because somehow
it's thinking that less-than symbol in the JS FOR line is an opening
of a tag.

Any suggestions on what I can do to reformulate that line to fix that?
Thanks,
Liam
 
T

Thomas 'PointedEars' Lahn

Stefan said:
<script type="text/javascript">
<![CDATA[

/* your script here */

//]]>
</script>

That is correct, but insufficient since he is likely serving XHTML as
text/html to make it MSHTML-compatible. And then, since the XHTML will be
regarded erroneous HTML and the HTML SCRIPT element's content model is CDATA
(not Parsed CDATA), the `<![CDATA[' will be passed verbatim to the script
engine, which will consider it a syntax error. Therefore,

// <![CDATA[

is required, too, or XHTML is to be served only with the most appropriate
media type, application/xhtml+xml, that triggers an XML parser and where you
don't need the `//' (but where they are not harmful, too).
You may want to consider using HTML instead of XHTML, where this is not
an issue.

The issue is only a different one, then: ETAGO delimiters, `</' must be
escaped in the script, then, especially if they are followed by a valid name
start character, or they would end the element content prematurely.
Many people (including me) think that XHTML offers no real advantage over
HTML,

But it does; unfortunately, good XHTML implementations were few and far
between, so most people never were brought to think about the advantages of
an XML-based document type, and treated XHTML as simply a modern form of
HTML. One notable advantage of XHTML over HTML is the possibility of
processing with applications of and for XML. The other is extensibility;
the possibility, fully realizable with XHTML 1.1, to use proprietary
elements and attributes in a fully *Valid* document. There is more, but it
is off-topic here. See also said:
and since no released version of Internet Explorer supports it at all, its
use on the web is restricted to non-Microsoft browsers anyway.

It is not, but serving XHTML to IE before version 7 is inadvisable.
(Someone -- IIRC it was either Andreas Prilop or Andreas Borutta -- reported
in de.comm.infosystems.www.authoring.misc that his IE 8 would support
application/xhtml+xml, but I could not confirm that as of yet.)


PointedEars
 
E

Eric Bednarz

Stefan Weiss said:
[…] In XML, the characters "<", ">", and "&" are special:

Erm, ‘>’ is only special if it is preceded by ‘]]’.
 
M

Mechphisto

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

That is correct, but insufficient since he is likely serving XHTML as
text/html to make it MSHTML-compatible.  And then, since the XHTML willbe
regarded erroneous HTML and the HTML SCRIPT element's content model is CDATA
(not Parsed CDATA), the `<![CDATA[' will be passed verbatim to the script
engine, which will consider it a syntax error.  Therefore,

[...snip...]

OK, I have to admit, most of this reply went right over my head.

I changed the page property to:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://
www.w3.org/TR/html4/loose.dtd">

and I'm not getting the previous errors anymore. (Just a hundred
warnings about the />'s in all my <br />s I'll need to fix.

Thanks,
Liam
 
S

Scott Sauyet

Mechphisto said:
Thomas said:
Stefan said:
<script type="text/javascript">
<![CDATA[
   /* your script here */
//]]>
</script>
That is correct, but insufficient since he is likely serving XHTML as
text/html to make it MSHTML-compatible.  And then, since the XHTML will be
regarded erroneous HTML and the HTML SCRIPT element's content model is CDATA
(not Parsed CDATA), the `<![CDATA[' will be passed verbatim to the script
engine, which will consider it a syntax error.  Therefore,

[...snip...]

OK, I have to admit, most of this reply went right over my head.

If you didn't understand Stefan's reply, it's probably worth reading
it again carefully so that you do understand it. Using XHTML on the
web has some significant downsides.

If you didn't understand Thomas' reply, there's some technical
information that may not be important to you, but there are two points
you should still take away from it:

* If you use a CDATA section in a script element, then you should
comment out it's opening "<![CDATA[" as well as its closing "]]>".
* Using XHTML on the web also has some significant upsides.

I changed the page property to:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

and I'm not getting the previous errors anymore. (Just a hundred
warnings about the />'s in all my <br />s I'll need to fix.

That will work, but if you have reason to do any processing of this as
an XML document, then using a XHTML format is very useful, and you can
remove that validation by wrapping your script content the way Thomas
suggested, or, probably better, by making the JS an external reference
rather than including it in the page.
 
T

Thomas 'PointedEars' Lahn

Mechphisto said:
I changed the page property to:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://
www.w3.org/TR/html4/loose.dtd">

That is a DOCTYPE declaration (for an HTML document), not a "page property".
and I'm not getting the previous errors anymore. (Just a hundred
warnings about the />'s in all my <br />s I'll need to fix.

Yes, you should remove the ` /' because `<br />' is equivalent to `<br>&gt;'
(but many UAs parse it like `<br>' instead.)

Also watch out for `</' (ETAGO delimiters) in SCRIPT elements; escape them
e.g. with `<\/', or move the script to an external resource that you include
with `<script ... src="...">...</script>'.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,077
Messages
2,570,566
Members
47,202
Latest member
misc.

Latest Threads

Top