" Kebahagiaan anda tumbuh berkembang manakala anda membantu orang lain. Namun, bilamana anda tidak mencoba membantu sesama, kebahagiaan akan layu dan mengering. Kebahagiaan bagaikan sebuah tanaman, harus disirami setiap hari dengan sikap dan tindakan memberi. "  —  J.Donald Walters
 

Avoid CSS/Javascript XHTML Document Error Using CDATA

more...

Based on W3C Recommendation Document on XHTML 1.0 (Review Section: C.4. Embedded Style Sheets and Scripts), XHTML document don’t allow you to have any embedded stylesheet and/or script which using < or & or ]] or -- . We should use external stylesheet and/or script if we use any of those symbols, so XHTML parser could parse the XHTML document correctly.

However if we can not use external script and/or stylesheet because of some reason, the W3C provides instruction on how to wrap javascript functions to avoid misinterpretation by the XML processor.

The solution is simple, it only requires the script be wrapped within a cdata marked section. We can escape it using CDATA. (W3C Recommendation Document on XHTML 1.0 – Review section: 4.8 Script and Style elements).

For example:

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

As a result, < and & will be treated as the start of markup, and entities such as &lt; and &amp; will be recognized as entity references by the XML processor to < and & respectively. And also if your XHTML documents validated with online W3C XHTML validator, it will also marked as valid.

As an addition, CDATA sections are recognized by the XML processor and appear as nodes in the Document Object Model. If you somehow need to add < or & character as an attribut value, you need to convert it to &lt; or &amp; respectively.

For example:

<script type="text/javascript" src="http://test.com/index.php?a=1&amp;b=2"></script>

Okay, that’s it. Hope this helps. :-)

0
Share up your minds and leave a comment
No one has commented yet. Be the first to comment!
Comment Form
XHTML Expert!
You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

* = required fields