These are my notes I made while researching this subject. There are many different ways to do the same thing.
Read this article first because it explains the different methods clearly: J3.x:Adding JavaScript and CSS to the page - Joomla!
Notes
- For your extension CSS and JS files to be overriden in a template you must use JHtml::stylesheet() and JHtml::script(). These functions have extra code in them that checks the various locations for files that would be allowed to override your files, and if present they do. In the end these 2 functions load addScript() and addStyleSheet() appropriately just with a different URL.
- As of Joomla 3.8, the majority of classes have been namespaced but with a fallback for when migrating to J4. So, you can still use JHtml::XXX, but the new approach is:
use Joomla\CMS\HTML\HTMLHelper; HTMLHelper::_('script', 'path/to/file.js'); HTMLHelper::_('stylesheet', 'path/to/file.css');
- I found this information here joomla 3.x - Where or how is the Jhtml class defined? - Joomla Stack Exchange
- the class can be found here libraries/src/HTML/HTMLHelper.php
- Most of the Joomla core classes are all now in libraries/src.
Examples
Different Methods I have found. Some might be dated but at least you know I have seen the same things.
/* Add CSS and JS to the <head> */ // Method 1 $document = JFactory::getDocument(); $document->addStyleSheet( JUri::root() . 'modules/mod_helloworld/css/helloworld.css' ); $document->addScript( JUri::root() . 'modules/mod_helloworld/js/helloworld.js' ); $document->addStyleSheet( JURI::base()."components/com_jdownloads/assets/rating/css/ajaxvote.css", 'text/css', null, array() ); $modules->doc->addStyleSheet($url . '/modules/mod_easyblogticker/assets/styles/ticker-style.css'); $doc->addStyleSheet(JURI::base().'plugins/content/maogalleryview/css/maogalleryview.css', $type = 'text/css', $media = 'screen,projection'); $doc->addScript(JURI::base().'plugins/content/maogalleryview/js/slider.mini.js', 'text/javascript'); // Method 2 JFactory::getDocument()->addStyleSheet( ltrim($mtconf->get('relative_path_to_js'),'/') . 'jquery.typeahead.css'); JFactory::getDocument()->addScript( ltrim($mtconf->get('relative_path_to_js'),'/') . 'jquery.typeahead.min.js'); // Method 3 - This allows overriding JHtml::stylesheet('mod_helloworld/css/helloworld.css', array(), true); JHtml::script('mod_helloworld/js/helloworld.js', false, true); JHtml::script('com_joomlaupdate/default.js', false, true, false); /* Misc */ // Method 1 - I found this in a template default.php and have not tested it echo JHtml::stylesheet('mod_mt_filter/mod_mt_filter.css',array(),true, false);
Use these in your extensions
// Add CSS and JS to the <head> - This method allows overriding JHtml::stylesheet('mod_helloworld/css/helloworld.css', array(), true); JHtml::script('mod_helloworld/js/helloworld.js', false, true);
References
Official Documentation
- J3.x:Adding JavaScript and CSS to the page - Joomla!
- Adding JavaScript - Joomla! Documentation
- J3.x:Adding JavaScript and CSS to the page - Joomla! Documentation
- J2.5:Adding JavaScript and CSS to the page - Joomla! Documentation (deprecated)
- CodeExample:JHtml::stylesheet - Joomla! Documentation - Describes the load order of files.
- Joomla! CMS 3.9 API » JoomlaCMSDocumentDocument - Document object and it's methods are described here. (addScript() and addStyleSheet())
- API17:JHtml::stylesheet - Joomla! Documentation (deprecated)
- Joomla! CMS 3.9 API » Joomla.CMS.HTML.HTMLHelper - HTML object and it's methods are described here.
3rd Party Articles
- Adding Files
- php - How to include external js file in joomla 3.8 - Stack Overflow - Not sure these are correct format.
- How to add JS/CSS files to Joomla modules? - Stack Overflow - Some basic errors explained and solved here.
- Difference Between Methods
- jfactory - Why use addStyleSheet or JHtml::stylesheet over just linking a CSS file? - Joomla Stack Exchange - Explains the difference betweent he 2 main methods. ie addStyleSheet() and addScript() add the files directly whereas JHtml::stylesheet() and JHtml::script() allow overriding.
- php - What's the difference between JHtml:script() and $doc->addScript? - Joomla Stack Exchange
- Misc
- difference between JURI::root and JURI::base ? - Joomla! Forum - community, help and support - This explains the slight difference between the 2.
- css - $document->addStyleSheet deprecated - Stack Overflow - This function is not actually deprecated but it has some variables that are.
- addStyleSheet, addScript and addSCriptDeclaration used into a plugins are positioned above those of the template and modules · Issue #6750 · joomla/joomla-cms · GitHub - I dont know if I will ever need this, so i have added it for reference only.