I developed a module recently that required certain JS and CSS files to be injected into the <head>
of each page. Initially, I used:
$doc =& JFactory::getDocument();
$doc->addScript(JURI::base(true).'/modules/mod_x/js/mod_x.js');
However, I noticed that when caching was enabled the site loaded both a cached version from /cache
and non-cached version from /modules/mod_x/js/
, causing JS errors.
When I changed this to:
JHtml::script(Juri::base().'modules/mod_x/js/mod_x.js');
The script was loaded only once, from cache, resolving these errors.
I read in the docs that the addScript
method was more 'low level' than the JHtml:script
method, but beyond saying that...
Of course, you will also need to manually code some of the steps [that JHtml:script does for you] that would be done automatically using the [JHtml:script method] above [if you use addScript].
...no further detail is given.
So what's the difference? I assume JHtml:script
eventually calls addScript
on a document object at some point, but are other intermediary steps taken first?