Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Ravens PHP Scripts And Web Hosting Forum Index -> RN FAQ
Author Message
Raven
Site Admin/Owner



Joined: Aug 27, 2002
Posts: 17087

PostPosted: Tue Mar 13, 2007 10:20 pm Reply with quote

Making JavaScript Compatible with XHTML

See this post for the background to this FAQ: Javascript Stopped

XHTML is subject to the same syntactical rules as XML. Because of this, an XHTML processor treats the characters &lt; < and &amp; & as markup, even if they reside inside a script block. Since the < and & characters are also used by the JavaScript language, this creates a conflict. When an XHTML processor sees these characters within the JavaScript code of a script block, it attempts to parse the JavaScript code as if it were markup, which causes the XHTML parser to fail.

You can get around this conflict and make all JavaScript code compatible with XHTML by placing the JavaScript code within a CDATA section. A CDATA section in XML/XHTML starts with the characters <![CDATA[ and ends with the characters ]]>.

Any characters within the starting and ending element of a CDATA section are not treated by the XML/XHTML processor as markup, thus preventing a conflict.

Here is an example of how to declare JavaScript code within a CDATA section so that it is compatible with XHTML. Note that I have intentionally misspelled script as scr!pt to avoid a conflict with NukeSentinel(tm).

<scr!pt type="text/javascript">
//<![CDATA[

alert("<This is compatible with XHTML>");

//]]>
</scr!pt>

Note that JavaScript source code must be placed within the opening and closing elements of the [b]CDATA
section. The CDATA section itself should be commented out with a JavaScript single-line comment // as in the example above. This is so that the JavaScript interpreter does not interpret the CDATA markup as JavaScript, which would cause a JavaScript error.

JavaScript code that is imported into an XHTML document from an external source file is always compatible with XHTML. So, for example, any code you place into the external file external.js and import into an XHTML file via the src attribute of the script tag will be valid. Here is an example of how to import the file external.js:

<scr!pt type="text/javascript" src="external.js"></scr!pt>
 
View user's profile Send private message
kguske
Site Admin



Joined: Jun 04, 2004
Posts: 6422

PostPosted: Wed Mar 14, 2007 6:13 am Reply with quote

Nice - thanks!

_________________
I google, therefore I exist...
nukeSEO - nukeFEED - nukePIE - nukeSPAM - nukeWYSIWYG
 
View user's profile Send private message
Display posts from previous:       
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Ravens PHP Scripts And Web Hosting Forum Index -> RN FAQ

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©