1

I'm trying to write some JavaScript code in a gxp file and I get a parsing exception at the "<" character in the "if" construct. Any ideas how to escape it?

<script type="text/JavaScript">
 var current = 0;
 var values = [];

 function goNext() {
  if (current < values.length - 1) {
   current = current + 1;
   update();
  }
 }
</script>

org.xml.sax.SAXParseException: The content of elements must consist of well-formed character data or markup.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Ada
  • 31
  • 1

4 Answers4

2

Put it in a <![CDATA[ ...... ]]> and then try

<script type="text/javascript">
//<![CDATA[
..
..
your code
..
..
//]]>
</script>
Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
  • 1
    Beat me to it. See http://stackoverflow.com/questions/66837/when-is-a-cdata-section-necessary-within-a-script-tag for more info. – Kevin Mark Jun 09 '11 at 18:08
2

Use

if (values.length - 1 > current) { 

}

:)

Krishna K
  • 1,925
  • 1
  • 14
  • 11
1

< is an XML entity.

Hide the Javascript code from XML:

<script type="text/javascript"><![CDATA[
 var current = 0;
 var values = [];

 function goNext() {
  if (current < values.length - 1) {
   current = current + 1;
   update();
  }
 }
// ]]></script>
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

This is because your code is "not protected" by a section.

you should try

<script type="text/JavaScript"><![CDATA[
var current = 0;
 var values = [];

 function goNext() {
  if (current < values.length - 1) {
   current = current + 1;
   update();
  }
 }
]]></script>

This way the "<" won't be treated as XML.

sitifensys
  • 2,024
  • 16
  • 29