miercuri, 2 septembrie 2015

[OmniFaces utilities 2.2] Add given JavaScript code as inline script to end of body of the current view


[OmniFaces utilities] The addScriptToBody() adds the given JavaScript code as inline script to end of body of the current view.
Note: This doesn't have any effect during ajax postbacks. Rather use Ajax#oncomplete(String...).

Method:

Usage:

Whenever you need to programmatically add inline a snippet of JavaScript code in the <body> section, you can use the Components#addScriptToBody() method. For example, let's suppose that we have the following dummy snippet of JavaScript code:

alert('I was added by the Components#addScriptToBody() utility method !')

Further, in an managed bean action method (or other JSF artifact) we can instruct JSF to add the given JavaScript code as inline script to end of body of the current view:

import org.omnifaces.util.Components;
...
String snippetOfJS = "alert('I was added by the Components#addScriptToBody() utility method !')";
UIComponent comp = Components.addScriptToBody(snippetOfJS);

The inline code is added in the page as below (just check the page source code to see the below markup):

<html xmlns ...>
 ...
 <body>
  ...
  <script type="text/javascript">alert('I was added by the Components#addScriptToBody() utility method !')</script>
 </body>
</html>

You may also want to read:

Niciun comentariu:

Trimiteți un comentariu