In our JSF 2.0 application at work, we include several javascript files via <h:outputscript>.
<h:outputScript library="javascript" name="DoStuff.js"/>
The resulting html references them as ‘text/javascript’.
<script type="text/javascript" src="/mycontext/javax.faces.resource/DoStuff.js.jsf?ln=javascript"></script>
According to this question, “text/javascript” is obsolete, what’s more, htmlunit complains about the type rather verbosely.
Of course, everything works just fine and I could shut off htmlunit’s logging, but I’d rather have JSF generate the correct type.
Is there a way to override the type chosen by <h:outputscript>?
This is hardcoded in the default renderer of the
<h:outputScript>. Assuming that you’re using Mojarra, it’s thecom.sun.faces.renderkit.html_basic.ScriptRenderer. According to the source, thetypeattribute is been set in thestartElementmethod. You could just override it:Or if you want to provide the enduser the possibility to specify the
typeattribute itself and default toapplication/javascriptwhen unspecified:To get it to run, register it as follows in
faces-config.xml:There’s by the way also the nice
@FacesRendererannotation which should work as followsHowever, when it’s already been definied by a standard renderer (the
ScriptRenderer!), then the custom one will fail to override it by a@FacesRenderer. See also issue 1748.