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

As an extension of this question, I'm trying to insert Javascript to a <h:commandButton />'s onclick property as action is already rendering an ajax table.

What I want to do: Get the selected items in a list box and turn them into parameters to be used in a JSF FileServlet. i.e. para2=value1&param=value2&param=value3

Here's what I have:

<script type ="text/javascript">
function myScript() {
    var box = document.getElementbyId('myForm:box');
    var length = box.options.length;
    var paramstring = "";
    for (var i = 0; i < 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 I get when page is loaded: javax.servlet.ServletException: Error Parsing /page.xhtml: Error Traced[line:15] The 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 soon as I add in for (var i = 0; i < length; i++) or even for (var i = 0; i < 10; i++) the page wouldn't load. Why does it not like the for loop?

This question is related to javascript jsf jsf-2 xhtml facelets

The answer is


I ran across this post today as I was running into the same issue and had the same problem of the javascript not running with the CDATA tags listed above. I corrected the CDATA tags to look like:

<script type="text/javascript">
//<![CDATA[ 

your javascript code here

//]]>
</script>

Then everything worked perfectly!


I had a git conflict left in my workspace.xml i.e.

<<<<———————HEAD

which caused the unknown tag error. It is a bit annoying that it doesn’t name the file.


Sometimes you will need this :

 /*<![CDATA[*/
 /*]]>*/

and not only this :

 <![CDATA[
 ]]>

I solved this converting the JSP from XHTML to HTML, doing this in the begining:

<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
...

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to jsf

Identifying and solving javax.el.PropertyNotFoundException: Target Unreachable WELD-001408: Unsatisfied dependencies for type Customer with qualifiers @Default Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes Target Unreachable, identifier resolved to null in JSF 2.2 Execution order of events when pressing PrimeFaces p:commandButton selectOneMenu ajax events The entity name must immediately follow the '&' in the entity reference java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config Primefaces valueChangeListener or <p:ajax listener not firing for p:selectOneMenu How does the 'binding' attribute work in JSF? When and how should it be used?

Examples related to jsf-2

Execution order of events when pressing PrimeFaces p:commandButton How to use if, else condition in jsf to display image Primefaces valueChangeListener or <p:ajax listener not firing for p:selectOneMenu How does the 'binding' attribute work in JSF? When and how should it be used? Difference between h:button and h:commandButton What is the JSF resource library for and how should it be used? List of <p:ajax> events Can I update a JSF component from a JSF backing bean method? How to provide a file download from a JSF backing bean? How to use PrimeFaces p:fileUpload? Listener method is never invoked or UploadedFile is null / throws an error / not usable

Examples related to xhtml

How to refresh table contents in div using jquery/ajax Uses for the '&quot;' entity in HTML The entity name must immediately follow the '&' in the entity reference Can I save input from form to .txt in HTML, using JAVASCRIPT/jQuery, and then use it? How to vertically align a html radio button to it's label? Image height and width not working? Removing border from table cells Enable & Disable a Div and its elements in Javascript how to remove the bold from a headline? How to make div occupy remaining height?

Examples related to facelets

The entity name must immediately follow the '&' in the entity reference Opening XML page shows "This XML file does not appear to have any style information associated with it." How to remove border from specific PrimeFaces p:panelGrid? How to use && in EL boolean expressions in Facelets? How to include another XHTML in XHTML using JSF 2.0 Facelets? Error parsing XHTML: The content of elements must consist of well-formed character data or markup How do I insert non breaking space character &nbsp; in a JSF page? Get Request and Session Parameters and Attributes from JSF pages How to display my application's errors in JSF?