The method itself isn't deprecated, only the format of the function parameters.
Earlier Joomla versions (deprecated):
$doc->addScript($url, $mime, $defer, $async)
Later Joomla versions:
$doc->addScript($url, $options, $attributes)
I'm not sure what the JHtml
deprecation notice is about but it shouldn't affect what you're trying to do.
So to answer you actual question, the correct way to add Javascript to your site will be as following:
General:
JFactory::getDocument()->addScriptDeclaration('
alert(1);
// more Javascript here
');
File:
JFactory::getDocument()->addScript(
JUri::root() . 'templates/MY_TEMPLATE/js/file.js'
);
If this is for an extension if your own and you're importing a file, I'd suggest using the JHtml
approach, as it will allow users to override the file , along with some other additional perks:
Path to your JS file:
media/mod_something/js/file.js
Code:
JHtml::_('script', 'mod_something/file.js', array('version' => 'auto', 'relative' => true));
Notice that I'm using 'relative' => true
so the path can be shortened.
Hope this helps