0

I have below code in one (main) Facelets page,

<h:panelGroup rendered="true">  
            <ui:insert>
                <ui:include src="/includeSecondPage.xhtml" />
           </ui:insert>
</h:panelGroup>

Below is the content in includeSecondPage.xhtml page,

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core">
<head>
<script type="text/javascript">
/* <![CDATA[ */

   function myScript () 
   {
        alert("Inside myScript");
   }

   myScript();

    /* ]]> */
</script>
</head>
<f:view>
<body>

<h:form id="secondForm">
<ui:composition>

<h:outputText value="This panel is called using Component Control Component"></h:outputText>

</ui:composition>
</h:form>
</body>
</f:view>
</html>

My Java Script is not getting called in my includeSecondPage.xhtml. Alert box is not popping up in my first (main) page which includes this second page. And there are no Java Script errors in Java Script console.

1
  • javscript that inserted into xhtml pages via .innerHTML (i.e. from an ajax call) is parsed, you need to create a new element node and append it to the head. Commented Feb 14, 2013 at 10:38

1 Answer 1

2

Anything outside <ui:composition> is discarded during include. Any content outside <ui:composition> is only used by visual editors such as Dreamweaver and should actually only represent "fill up" content in such way so that the include content is "properly" visually represented. If you have looked closer at the JSF-generated HTML output by rightclick, View Source in browser, you'd have noticed that those parts are completely absent in the HTML output.

Put the include content inside <ui:composition>. If you aren't using a visual editor, then also just get rid of anything outside <ui:composition>. Here's how your entire include file can look like:

<ui:composition
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    <script type="text/javascript">
        // <![CDATA[

        function myScript () {
            alert("Inside myScript");
        }

        myScript();

        // ]]>
    </script>

    <h:outputText value="This panel is called using Component Control Component" />
</ui:composition>

See also:

Sign up to request clarification or add additional context in comments.

1 Comment

Works as per your answer. "If you have looked closer at the JSF-generated HTML output by rightclick, View Source in browser, you'd have noticed that those parts are completely absent in the HTML output." Yes. This I noticed.

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.