0

Calling JavaScript from JSF component as follows seems to be wrong. Can you tell me why?

<ui:define name="javascript">
    <script type="text/javascript">

            function myFunction( message ){
               alert( message);
            }   

    </script>
</ui:define>

<h:commandButton id="bas" value="bas"
    onclick=" myFunction('call js Function'); " />

1 Answer 1

1

Two things are "not right":

  1. You need to put JS code in its own JS file and use <script src="js/foo.js"></script>. JS language namely contains operators which are illegal in XHTML based view technology such as Facelets, e.g. <, >, & and on. You would need to escape them or wrap it in a CDATA block which is plain ugly.

  2. If you're on JSF 2.0, you should be using <h:outputScript> instead of <ui:define> with a <script>.

    <h:outputScript library="js" name="foo.js" />
    <h:commandButton id="bas" value="bas" onclick="myFunction('call js Function');" />
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.