I'm facing some problems using CDATA blocks inside h:outputscripts with MyFaces 2.0... but I don't know exactly if I should avoid using CDATA with JSF2 or if it is because I do things wrong.
Maybe it is because I use many scripts in many Composite components...
The fact is that when I have some composite components that contain scripts with surrounded by CDATA blocks, other scripts in the page doen't work.
Removing CDATAs solves the issue.
Nevertheless I've had an issue where using CDATA blocks made some of my Composite components bug saying a property of the component can't be found on class NamingContainer when trying to render the component using f:ajax render attribute. Here is the workaround.
Removing the CDATA surrounding my scripts solved the problem.
So my question is : am I the only one having troubles with CDATA blocks and JSF2 (MyFaces) ?
0
votes
Why don't you put scripts in a separate file?
– Bhesh Gurung
I've just tried, but it doesn't seem to work. In fact my script targets components of a composite component and I use #{cc.clientID} in the script. So I think that when externalized, the js doesn't replace #{cc.clientID} by the real value...<br /> Is there a way for an externalized js to get it ?
– boblemar
2 Answers
1
votes
This is a known issue. See MYFACES-3339 for details. It was already fixed, so you can try the latest code HERE, and it will be included on 2.1.4 and 2.0.10.
1
votes
I can't answer if you're the only one having problems with it. I can at least answer that having JS code plain in a XML file is a poor practice. JS code is not well formed XML. Fiddling with escaping XML-special characters in JS code or putting JS code in CDATA blocks is plain ugly. That it gives troubles in JSF ajax response is in turn a different story. Technically, that would have been a bug in the JSF implementation used. But from the other side on, you're actually practicing a poor practice.
Just put JS code in its own .js
file which you reference by <h:outputScript>
.
<cc:implementation>
<h:outputScript library="foo" name="js/your-cc-script.js" target="head" />
...
</cc:implementation>