Tuesday, January 4, 2011

CDATA in JavaScript in XHTML


Example 1: Script between  <! [CDATA[  and  ]]>  is ignored
-------------------------------------------------------------------
<script type="text/javascript">
<! [CDATA[
alert('Javascript working!');
   ]]>

</script>
-----------------------------------------------------------
Example 2: CDATA is commented out, JavaScript returns to work
----------------------------------------------------------------------
<script type="text/javascript">
 <!--//--><![CDATA[//><!--
alert('Javascript working!');
 //--><!]]>
</script>
----------------------------------------------------------------------------
Example 3:  CDATA is commented out, JavaScript returns to work
-----------------------------------------------------------------------------
<center style="background-color:green"><form NAME="clock_form">
<input TYPE=TEXT NAME="banner" SIZE=50>
</FORM></center>
<script type="text/javascript">
 <!--//--><![CDATA[//><!--
var position=0;
var msg=" Hi, thank you for visiting my blog. Happy New Year! Have a nice day! "
var i = 0;
while (i ++ < 10)
    msg = " "+msg;
function banner() {

  document.clock_form.banner.value=msg.substring(position,position+50);

  if(position++==msg.length) {

      position=0;
  }
  setTimeout("banner()",100);
}
banner();

 //--><!]]>
</script>

---------------------------------------------
Result of example 1: Alert is not shown.

Result of example 3: Javascript is working



No comments:

Post a Comment