Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JS comparison in JSF

 
Celia Xu
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I have a jsp page and in my <f:view> tag I want to put my JS :
for example:


i tried to put it either between<head></head> or <body></body> (also tried put <f:verbatim> around)
BUT when i inspect element i found when I use "<", all the lines after that in js messed up:

because of this, it will throw errors if i want to use this function.
However, it will be fine if i use any other comparison operators.
anyone has idea?
 
Nikhil Padharia
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use Ampersand - & lt; in place of < for comparision.
All four letters should be clubbed together.. forum is automatically converting it to < if i post such..
 
Tim Holloway
Saloon Keeper
Pie
Posts: 17620
39
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Celia!

The JSF View Definition Language (VDL) is XML and in XML, there are 5 "magical" characters: & ' " < &rt;. Because these characters are part of the XML "language" itself, it's possible to confuse the XML parser when you use them for non-XML purposes, such as inside JavaScript functions.

There are 3 ways to avoid this problem

1. Use XML entities, like Nikhil recommended. I think he ran into problems with our XML parser, so he had to mis-type slightly. The entity code for the ampersand character is actually "&" and please note that the final ";" character is required.

2. Use XML CDATA escapes. Like so:



Note the "//" comment marker before the CDATA close "]>". You're dealing with about 3 different syntax forms at the same time so things like this avoid having the wrong thing parsed at the wrong time.

3. Finally, the easiest solution of them all. Put the JavaScript in a separate Javascript-only resource file and include it using the HTML LINK directive or something similar. Since the js file is a completely different file being accessed from the client, it won't be parsed by the JSF view digester.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic