Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle < and > signs in javascript in .xhtml page?

I have a JSF 2 .xhtml page with javascript in the page code. In that javascript code, I have the folowing:

                    var lat1 = latlng1.lat();
                    var lat2 = latlng2.lat();
                    var minLat = lat1<lat2?lat1:lat2;
                    var maxLat = lat1<lat2?lat2:lat1;

I get a page error that complains about the < sign - apparently the parser thinks it is an xml type symbol, rather than a "less than" sign.

javax.servlet.ServletException: Error Parsing /index_2.xhtml: Error Traced[line: 87] Element type "lat2" must be followed by either attribute specifications, ">" or "/>".

How can I get around this?

I suspect I can probably move it into a javascript library, but I am just trying to do a simple test to see if I can do what I am trying to accomplish.

Thanks.

like image 510
Burferd Avatar asked Dec 27 '22 05:12

Burferd


1 Answers

Put it in a CDATA-section:

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

 var lat1 = latlng1.lat();
 var lat2 = latlng2.lat();
 var minLat = lat1<lat2?lat1:lat2;
 var maxLat = lat1<lat2?lat2:lat1;

/* ]]> */
</script>

...otherwise a XML-parser will interpret the < as the start of an element. CDATA will not be parsed.

like image 140
Dr.Molle Avatar answered Jan 11 '23 16:01

Dr.Molle