Tuesday, May 12, 2009

JavaScript and Facelets

Did you know that you're not allowed to use the full syntax of JavaScript language with Facelets? Well, I didn't and it took me over 4 hours to find that out. Let me explain:

Here's a simple JS function that will cause the rendering engine swallow it's own tongue and spit a nasty error message:

<script>
function doSomething(items) {
if(items.length > 0 && items.length < 5)
return;
}
</script>

First of all the < and > are not valid characters in XML so they are not valid in Facelets as well. And if you substitute them by &lt; it will be rendered as &lt; which makes the usage of < character impossible.
The same situation is with > sign as well as with && operator.

Of course you might say that my doSomething function makes no sense - it's for educational purposes only!

May the spirits protect us from Facelets!

6 comments:

  1. Actually there's a preatty simple way of dealling with JSF reading what it shouldn't...

    Comment it out using html (stragelly your blog says my html is server side????)


    Almost pulled my hairs out figuring that out

    ReplyDelete
  2. Ohhh by the way... Love some of the things you're talking about... Noticed some of those thing that make you say out loud WTF.

    Hope you don't mind me beeing the devils advocat... After all It's talking about things that makes us evolve

    ReplyDelete
  3. You can use CDATA to be able to use < , > and & in Javascript and Facelets... no problem

    ReplyDelete
  4. took me four hours too
    WTF WTF WTFFFFFFFF!!!

    ReplyDelete
  5. hey I am using CDATA as well, and while the > < work many other js functions dont! This is so frustrating!

    Oh and I am confused to where I use ' or ":
    document.createElement('button'); or
    document.createElement("button"); ??

    ReplyDelete
  6. http://www.codehouse.com/javascript/tips/xhtml/

    ReplyDelete