-
Available since OmniFaces 1.8
The <o:deferredScript>
is a component based on the standard <h:outputScript>
which defers the loading of the given script resource to the window load event. In other words, the given script resource is only loaded when the window is really finished with loading. So, the enduser can start working with the webpage without waiting for the additional scripts to be loaded. Usually, it are those kind of scripts which are just for progressive enhancement and thus not essential for the functioning of the webpage.
This will give bonus points with among others the Google PageSpeed tool, on the contrary to placing the script at bottom of body, or using defer="true"
or even async="true"
.
Since 2.4 this will render the crossorigin
attribute with a value of anonymous
. Since 3.13 this will also render the integrity
attribute with a base64 encoded sha384 hash as SRI, see also MDN.
Usage
Just use it the same way as a <h:outputScript>
, with a library
and name
.
<o:deferredScript library="yourlibrary" name="scripts/filename.js" />
You can use the optional onbegin
, onsuccess
and onerror
attributes to declare JavaScript code which needs to be executed respectively right before the script is loaded, right after the script is successfully loaded, and/or when the script loading failed.
Demo
This component is also
used
on this showcase web application on the two JS files
prettify.js and
onload.js.
Rightclick the page and View Source and find the OmniFaces.DeferredScript.add
call at the bottom of <body>
.
Note that this showcase application is also using the
CombinedResourceHandler
which transparently recognizes and combines the deferred script resources into a single resource as well.
Using <o:deferredScript>
on PrimeFaces JavaScript resources
Refer to the following Stack Overflow Question&Answer for a detailed guide: Defer loading and parsing of PrimeFaces JavaScript files.
VDL documentation
API documentation
Java source code
org.omnifaces.component.script.DeferredScript
org.omnifaces.component.script.ScriptFamily
org.omnifaces.renderer.DeferredScriptRenderer