How do I disable the loading of
mootools-core.js
core.js
In <head>
without using a third party component/plugin?
Im using Joomla 3.2, at the top of my tempate I put:
$doc = JFactory::getDocument();
$dontInclude = array(
'/media/jui/js/jquery.js',
'/media/jui/js/jquery.min.js',
'/media/jui/js/jquery-noconflict.js',
'/media/jui/js/jquery-migrate.js',
'/media/jui/js/jquery-migrate.min.js',
'/media/jui/js/bootstrap.js',
'/media/system/js/core-uncompressed.js',
'/media/system/js/tabs-state.js',
'/media/system/js/core.js',
'/media/system/js/mootools-core.js',
'/media/system/js/mootools-core-uncompressed.js',
);
foreach($doc->_scripts as $key => $script){
if(in_array($key, $dontInclude)){
unset($doc->_scripts[$key]);
}
}
This prevents those scripts from being included, add or remove scripts as needed
JURI::root(true).
before the script paths and it worked.
You can unset it in the template
unset(
$this->_scripts['/media/jui/js/mootools-core.js'],
$this->_scripts['/media/jui/js/core.js']);
This will remove the both JS files from the Header.
In Joomla 3.x, you can call JHtml::_('jquery.framework')
in your template to load jQuery without Mootools. This loads jQuery in no conflict mode by default so if you need normal mode, just add a second parameter of false: JHtml::_('jquery.framework', false)
.
I've used this:
foreach ($doc->_scripts as $script => $value)
{
if (preg_match('/media\/jui/i', $script))
unset($doc->_scripts[$script]);
}
needle
string is not variable and the only intention is to make a case-insensitive match, then it would be better practice to use stripos() ... if (stripos($script, 'media/jui') !== false)
Commented
Nov 30, 2018 at 0:40
The solution by @ContextSwitch wasnt working for me due to some scripts were being loaded after the template was processed so unsetting the files at that stage had no effect on those files.
So I had to implement the similar solution but using a simple plugin bound to onBeforeCompileHead
event. As described by me here.