login/register

Snip!t from collection of Alan Dix

see all channels for Alan Dix

Snip
summary

Including Valid Inline Javascript in XHTML
You may notice that your XHTML does not validate because ...
<script type="text/javascript">
//<![CDATA[
... ndering the page requires functions from the javascript.

» Including Javascript in XHTML: external, DOM created, CDATA & comments :: CSS, JavaScript and XHTML Explained
http://www.evotech.net/blog/2007/05/including-javascript-in-xhtml/

Categories

/Channels/techie/JavaScript

[ go to category ]

For Snip

loading snip actions ...

For Page

loading url actions ...

Including Valid Inline Javascript in XHTML

You may notice that your XHTML does not validate because of code in your javascript. Here is the correct way to include an internal javascript file:

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

		code here

//]]>
</script>
  • In XHTML, the type attribute is a required attribute. 
  • Do not include the language attribute, unless you’re including something other than javascript.
  • CDATA is required in XHTML because using the characters <, >, & and " as characters rather than entities will not validate, and Javascript would not understand it if you coded something as if (A &gt; B){}
  • The old fashioned method of hiding the script from browsers of commenting out the javascript, seen on most websites, is useless today as all browsers understand the script tag.  All browsers either support javascript or are smart enough to ignore the content between the script tag if they don’t support javascript:
    <script type="text/javascript">
    <!-- Hiding this way is no longer necessary
    //-->
    </script>
    
  • However, not all browsers support XHTML and therefore may not understand <![CDATA[

        ]]>, so hiding the CDATA from non-XHTML browsers with the single line javascript comment of //, allowes XHTML compliant browsers to except special characters within the CDATA, and non-compliant browsers simply ignore the CDATA call since it’s part of a comment.

  • The best place to put it is right before the end of the body unless rendering the page requires functions from the javascript.

HTML

<h3 id="js_o4">Including Valid Inline Javascript in XHTML</h3> <p>You may notice that your XHTML does not validate because of code in your javascript. Here is the correct way to include an internal javascript file:</p> <pre><code>&lt;script type="text/javascript"&gt; //&lt;![CDATA[ code here //]]&gt; &lt;/script&gt;</code></pre> <ul> <li>In XHTML, the <code>type</code> attribute is a <strong>required</strong> attribute.&nbsp; </li> <li>Do not include the <code>language</code> attribute, unless you&#x2019;re including something other than javascript. </li> <li>CDATA is required in XHTML because using the characters <code>&lt;, &gt;, &amp;</code> and <code>"</code> as characters rather than entities will not validate, and Javascript would not understand it if you coded something as <code>if (A &amp;gt; B){}</code> </li> <li>The old fashioned method of hiding the script from browsers of commenting out the javascript, seen on most websites, is useless today as all browsers understand the script tag.&nbsp; All browsers either support javascript or are smart enough to ignore the content between the script tag if they don&#x2019;t support javascript: <pre>&lt;script type="text/javascript"&gt; &lt;!-- Hiding this way is no longer necessary //--&gt; &lt;/script&gt; </pre> </li> <li>However, not all browsers support XHTML and therefore may not understand <code>&lt;![CDATA[ </code><p> &nbsp;&nbsp;&nbsp; ]]&gt;, so hiding the <code>CDATA</code> from non-XHTML browsers with the single line javascript comment of <code>//</code>, allowes XHTML compliant browsers to except special characters within the <code>CDATA</code>, and non-compliant browsers simply ignore the <code>CDATA</code> call since it&#x2019;s part of a comment. </p></li><li>The best place to put it is right before the end of the <code>body</code> unless rendering the page requires functions from the javascript. </li></ul>