h:outputScript

HtmlOutputScript is a UIOutput component that renders a <script> element with type="text/javascript".

General Usage

The script can be rendered in different positions on the page via the target attribute. Valid values include body, form, and head. The default value of this attribute is an empty (null) value which causes the <script> to be rendered inline (corresponding to its position in the view). Specifying body for the target attribute will cause the <script> to be rendered as close to the closing body tag as possible.
This <script> is rendered inline because the target attribute is not specified. The drawback of inline scripts is that they block rendering of the rest of the page.
This <script> is rendered immediately before the closing </body> tag because target=body. The benefit of rendering scripts before the closing </body> tag is that they do not block rendering of the rest of the page.

Source Code

  1. <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html"
  2. xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
  3.  
  4. <!-- Example 1: Script rendered inline -->
  5. <h:form>
  6. <h:panelGroup >
  7. <span id="example1:text"></span>
  8. </h:panelGroup>
  9. <h:outputScript >
  10. document.getElementById('example1:text').innerHTML = '#{i18n['h-outputscript-general-script-1']}';
  11. </h:outputScript>
  12. </h:form>
  13.  
  14. <!-- Example 2: Script rendered at the bottom of the page -->
  15. <span id="example2:text"></span>
  16. <h:outputScript target="body">
  17. document.getElementById('example2:text').innerHTML = '#{i18n['h-outputscript-general-script-2']}';
  18. </h:outputScript>
  19.  
  20. </ui:composition>
Liferay Faces Bridge Implementation 5.0.0 + Showcase Common 3.1.1 + Liferay Faces Util 3.4.1 + Mojarra 2.2.20