Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Oct 2008
    Location
    Poland
    Posts
    382
    Thanks
    150
    Thanked 2 Times in 2 Posts

    XHTML validation - please help

    I need to validate a page with this code:

    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
    Code:
    &&
    to
    Code:
    &amp;&amp;
    the code doesn't work. What do you suggest?

  • #2
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,444
    Thanks
    11
    Thanked 308 Times in 307 Posts
    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:
    Code:
    <script type="text/javascript">
    <![CDATA[
        ...code...
    ]]>
    </script>
    Dave

  • #3
    New to the CF scene
    Join Date
    Oct 2011
    Location
    UK
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tracknut View Post
    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:
    Code:
    <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.

  • #4
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,387
    Thanks
    32
    Thanked 288 Times in 282 Posts
    Quote Originally Posted by utnalove View Post
    The validator says that: character "&" is the first character of a delimiter but occurred as data
    In 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.

    Quote Originally Posted by utnalove View Post
    But if I change the
    Code:
    &&
    to
    Code:
    &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.

    Quote Originally Posted by tracknut View Post
    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:
    Code:
    <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:

    Code:
    <script type="text/javascript">
    	// <![CDATA[
    	// do something
    	// ]]>
    </script>
    If using XHTML5, this can be shortened to:

    Code:
    <script>
    	// <![CDATA[
    	// do something
    	// ]]>
    </script>
    Last edited by Arbitrator; 10-28-2011 at 06:34 AM. Reason: I fixed a few typos.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • Users who have thanked Arbitrator for this post:

    utnalove (10-28-2011)

  • #5
    Regular Coder
    Join Date
    Oct 2008
    Location
    Poland
    Posts
    382
    Thanks
    150
    Thanked 2 Times in 2 Posts
    Thanks for your reply, the site is a wordpress blog with the following:

    Code:
    <!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:

    Code:
    <script type="text/javascript">
    	// <![CDATA[
    	// do something
    	// ]]>
    </script>
    and it looks like it works. Did I do the right thing?

  • #6
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,387
    Thanks
    32
    Thanked 288 Times in 282 Posts
    Quote Originally Posted by utnalove View Post
    the site is a wordpress blog with the following:

    Code:
    <!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.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • Users who have thanked Arbitrator for this post:

    utnalove (10-28-2011)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •