I'm using $doc->addScript($path.'/js/script.js');
to load a Javascript file inside the head section. Is there any native way to load it in the bottom of the page, before the closing </body>
tag?
Thanks!
3 Answers
No there isn't.
addScript(string $url, string $type = "text/javascript", boolean $defer = false, boolean $async = false)
You can use $defer and $async parameters of the addScript method to load JavaScript after the content.
Example:
$doc->addScript($path.'/js/script.js', 'text/javascript', true, false);
Use $defer = true if you want to load scripts in order, i.e. if one script is dependant on another.
$doc->addScript($path.'/js/script.js', 'text/javascript', false, true);
Use $async = true to load scripts asynchronously and when the order in which they are loaded does not matter
If you've just landed here via google see answer to this question here: