• Post Reply Bookmark Topic Watch Topic
  • New Topic

Embedding Javascript in a facelet Page  RSS feed

 
Abiodun Adisa
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have defined a Javascript function in the middle of my facelet page


But whenever the page is generated and i look into the HTML all i can see is




which means the Javascript code is getting lost, Please how do i resolve this
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Facelets uses XHTML as markup language which on its turn is based on XML.

The above is not only invalid XML syntax, but also a bad practice. You shouldn't tight couple scripting with presentation. Place Javascript code in its own .js file which you load in the head. If you want to include scripting in presentation at any way, then in case of XHTML you need to wrap it with XML CDATA tags.
 
Abiodun Adisa
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bauke Scholtz wrote:Facelets uses XHTML as markup language which on its turn is based on XML.

The above is not only invalid XML syntax, but also a bad practice. You shouldn't tight couple scripting with presentation. Place Javascript code in its own .js file which you load in the head. If you want to include scripting in presentation at any way, then in case of XHTML you need to wrap it with XML CDATA tags.


I embedded it using CDATA but it still does not show
 
Tim Holloway
Bartender
Posts: 18662
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wrap it in a "f:verbatim" tag. Also be careful where you place it - A good place in a facelets composition is right up top after the ui:define's but before the content. DON'T put it outside the ui:composition, since that stuff all gets discarded by definition.

CDATA is highly recommended, but not essential. In your case, it wouldn't have mattered. CDATA is what keeps you from getting into trouble when you have relational operators (<, >) and other XML tokens in your javascript code. You could also use the XML entities and avoid CDATA, but that's kind of pointless. And ugly.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!