...

View Full Version : XHTML validation - please help



utnalove
10-27-2011, 11:20 PM
I need to validate a page with this code:


<script type="text/javascript">
if ('standalone' in navigator && !navigator.standalone && (/iphone|ipod|ipad/gi).test(navigator.platform) && (/Safari/i).test(navigator.appVersion)) {
document.write('<link rel="stylesheet" href="http://site.com/style/add2home.css">');
document.write('<script type="application/javascript" src="http://site.com/src/add2home.js"><\/s' + 'cript>');
}
</script>

The validator says that: character "&" is the first character of a delimiter but occurred as data

But if I change the
&& to
&amp;&amp; the code doesn't work. What do you suggest?

tracknut
10-28-2011, 12:34 AM
You could either ignore it (I'm not aware any recent browser will have an issue the way you have it), or you could put the JS code inside CDATA tags:


<script type="text/javascript">
<![CDATA[
...code...
]]>
</script>


Dave

jennysmith999
10-28-2011, 06:27 AM
You could either ignore it (I'm not aware any recent browser will have an issue the way you have it), or you could put the JS code inside CDATA tags:


<script type="text/javascript">
<![CDATA[
...code...
]]>
</script>


Dave
I am trying to solve my same problem according to your system. But Firefox has some problems with this.

Arbitrator
10-28-2011, 06:31 AM
The validator says that: character "&" is the first character of a delimiter but occurred as dataIn XML, script elements are not CDATA elements (as in HTML) and, therefore, entities—which begin with the ampersand (&) character—are allowed and interpreted as such. (An example entity is &amp; which begins with &, has the entity name amp, and is terminated by ;.)

Entity names cannot contain the entity start character (i.e., &) and, thus, your code && is malformed and should generate an XML error.

You're probably rendering your document with the HTML MIME type or a *.html file extension and therefore your document does not generate a XML errors outside of a validator. Try *.xhtml, *.xht, or *.xml file extensions, and you will encounter such errors.


But if I change the
&& to
&amp;&amp; the code doesn't work. What do you suggest?This is because &amp;&amp; generates other errors: this code is passed to the script parser which will throw an error because &amp;&amp; is not a valid ECMAScript/JavaScript operator.


You could either ignore it (I'm not aware any recent browser will have an issue the way you have it), or you could put the JS code inside CDATA tags:


<script type="text/javascript">
<![CDATA[
...code...
]]>
</script>
To do things completely properly, you should escape the CDATA delimiters to ensure that they are not interpreted as script:


<script type="text/javascript">
// <![CDATA[
// do something
// ]]>
</script>

If using XHTML5, this can be shortened to:


<script>
// <![CDATA[
// do something
// ]]>
</script>

utnalove
10-28-2011, 09:13 AM
Thanks for your reply, the site is a wordpress blog with the following:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


I put the script in the following:


<script type="text/javascript">
// <![CDATA[
// do something
// ]]>
</script>

and it looks like it works. Did I do the right thing?

Arbitrator
10-28-2011, 02:24 PM
the site is a wordpress blog with the following:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

[...]

Did I do the right thing?Yes; the type attribute is required in XHTML1.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum