简体   繁体   中英

Calling JavaScript from JSF component seems wrong

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'); " />

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, eg < , > , & 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');" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM