3

I have found some weird behaviour after upgrading to Mojarra 2.2.3. The following Javascript declaration gets mangled:

In the .xhtml file:

<script type="text/javascript">
<!-- /* <![CDATA[ */
    $(document).ready(function() {                                                                             
        if ($('#some_identifier').size() > 0) 
        ...
/* ]]> */-->
</script>

This is mangled into the following nonsense:

<script type="text/javascript">
<!-- /* &lt;![CDATA[ */
    $(document).ready(function() {
        if ($('#some_identifier').size() &gt; 0)
        ...
/* ]]&gt; */-->
</script>

This breaks all javascript code embedded in .xhtml files. I verified that this does not happen with the versions we used previously (2.0.x), so I must assume it's got something to do with the new Mojarra version. Any ideas on how to fix this or work around it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Jürgen Simon
  • 876
  • 1
  • 12
  • 35

1 Answers1

5

This CDATA syntax is completely invalid. It's not clear where you got this from and why you thought it would be valid. Perhaps you confused it with CDATA syntax for CSS code. In any case, for proper CDATA syntax in JS code, carefully read this Mozilla Developer Network article: Writing JavaScript for XHTML.

The valid (modern) syntax is:

<script type="text/javascript">
  <![CDATA[
    $(document).ready(function() {                                                                             
      if ($('#some_identifier').size() > 0) 
        ...
  ]]>
</script>

If you really, really need to support old browsers which no one on the world uses, then use the following syntax which should work in those browsers which doesn't natively support JavaScript and therefore are incapable of parsing <script> elements (wonder yourself, would your JSF application, rich of JavaScript, still work on those webbrowsers? would it really be useful to escape JS for them as well?)

<script type="text/javascript">
  <!--//--><![CDATA[//><!--
    $(document).ready(function() {                                                                             
      if ($('#some_identifier').size() > 0) 
        ...
  //--><!]]>
</script>

By the way, much better is to put that JS code in its own .js file.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • The first syntax does not work (tested on Ubuntu with both Firefox and Google Chrome who both balk at it). The second variant seems to work. Thanks, BalusC. – Jürgen Simon Sep 17 '13 at 14:03
  • 1
    Apparently you're serving the HTML output using the wrong content type or document type causing those browsers to get confused. It should be `text/html`, which you can explicitly set by wrapping your master template with ``. Besides that, you should these days preferably use HTML5 document type ` `. Point is, explicitly tell the browser how to interpret the document instead of letting it to guess based on misinformation. – BalusC Sep 17 '13 at 14:03
  • I submit that the browser has nothing to do with it. What happened here, is that the content changed that was served by the app. I checked the results using curl and wget, simply looking at what comes out of the application. In the case of the above syntax, both tools showed the CDATA to be escaped. Ergo it's not a matter of browser interpretation. Somehow, the syntax used confuses JSF. The suspicion is furthered by the fact, that is only cropped up after updating to 2.2.3. – Jürgen Simon Sep 17 '13 at 14:10
  • I wasn't talking about your initial problem, but about the first syntax in my answer which apparently didn't work for you. It works for me on all machines and browsers (also with Mojarra 2.2.3). That it didn't work for you is most likely caused by serving with wrong content or document type. This is exactly what I tried to explain in my previous comment. If the first syntax did't work for you, then you've bigger problems. – BalusC Sep 17 '13 at 14:11
  • A misunderstanding.On the original syntax:as a specialist on the subject, do you have an idea if this could be a bug with the JSF implementation? It seems to me the syntax I used originally is used commonly, perhaps it would pay to file a bug report? – Jürgen Simon Sep 17 '13 at 14:13
  • 1
    The syntax you used originally is plain invalid. That more people made the same mistake doesn't make it a valid syntax. – BalusC Sep 17 '13 at 14:14
  • True. Just glad it's running again now. Thanks. – Jürgen Simon Sep 17 '13 at 14:17