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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Vikas V
  • 3,176
  • 2
  • 37
  • 60
  • 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. – philipp Feb 14 '13 at 10:38

1 Answers1

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 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. – Vikas V Feb 14 '13 at 11:45