javascript - Error parsing XHTML: The content of elements must consist of well-formed character data or markup -


as extension of question, i'm trying insert javascript <h:commandbutton />'s onclick property action rendering ajax table.

what want do: selected items in list box , turn them parameters used in jsf fileservlet. i.e. para2=value1&param=value2&param=value3

here's have:

<script type ="text/javascript"> function myscript() {     var box = document.getelementbyid('myform:box');     var length = box.options.length;     var paramstring = "";     (var = 0; < length; i++) {         if (i != (length - 1) {             if (box.options[i].selected) {                 paramstring = paramstring + "param=" + box.options[i].value + "&amp;";             }         } else {             paramstring = paramstring + "param=" + box.options[i].value;         }     }     if (document.getelementbyid('myform:checkbox').checked) {         window.location='fileservlet? + paramstring;     } } </script>   

what when page loaded: javax.servlet.servletexception: error parsing /page.xhtml: error traced[line:15] content of elements must consist of well-formed character data or markup.

what doesn't trigger exception:

<script type ="text/javascript"> function myscript() {     var box = document.getelementbyid('myform:box');     var length = box.options.length;     var paramstring = "";      if (document.getelementbyid('myform:checkbox').checked) {         window.location='fileservlet? + paramstring;     } } </script>  

as add in for (var = 0; < length; i++) or for (var = 0; < 10; i++) page wouldn't load. why not loop?

facelets xml based view technology uses xhtml+xml generate html output. xml has 5 special characters has special treatment xml parser:

  • < start of tag.
  • > end of tag.
  • " start , end of attribute value.
  • ' alternative start , end of attribute value.
  • & start of entity (which ends ;).

in case of <, xml parser implicitly looking tag name , end tag >. however, in particular case, using < javascript operator, not xml entity. totally explains xml parsing error got:

the content of elements must consist of well-formed character data or markup.

in essence, you're writing javascript code in wrong place, xml document instead of js file, should escaping xml special characters accordingly. < must escaped &lt;.

so, essentially, the

for (var = 0; < length; i++) { 

must become

for (var = 0; &lt; length; i++) { 

to make xml-valid.

however, makes javascript code harder read , maintain. stated in mozilla developer network's excellent document writing javascript xhtml, should placing javascript code in character data (cdata) block. thus, in jsf terms, be:

<h:outputscript>     <![cdata[         // ...     ]]> </h:outputscript> 

the xml parser interpret block's contents "plain vanilla" character data , not xml , hence interpret xml special characters "as-is".

but, better put js code in own js file include <script src>, or in jsf terms, <h:outputscript>.

<h:outputscript name="functions.js" target="head" /> 

this way don't need worry xml-special characters in js code.

see also:


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -