Question

I've recently upgraded from Joomla 3.2.1 to Joomla 3.2.2.

In Joomla 3.2.1, I manually unset jQuery from being included:

$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]);
    }
}

But this isn't working in Joomla 3.2.2. Is there a way to not include Joomla's jQuery in 3.2.2?

Was it helpful?

Solution 3

I've added:

            $doNotInclude = array(
                'jquery',
                'bootstrap',
                'behavior',
            );
            if(in_array($file, $doNotInclude)){
                return;
            }

immediately after:

            list($key, $prefix, $file, $func) = static::extract($key); 

in libraries/cms/html/html.php, in the "_" function.

I don't like it since its a modification to the Joomla core but it works. I'm still looking for a better solution.

OTHER TIPS

Another variation which works well for me with Joomla 3.4 is to edit the template > index.php file with something like:

$doc = JFactory::getDocument();

$headData = $doc->getHeadData();
$scripts = $headData['scripts'];

//scripts to remove, customise as required

unset($scripts[JUri::root(true) . '/media/system/js/mootools-core.js']);
unset($scripts[JUri::root(true) . '/media/system/js/mootools-more.js']);
unset($scripts[JUri::root(true) . '/media/system/js/core.js']);
unset($scripts[JUri::root(true) . '/media/system/js/modal.js']);
unset($scripts[JUri::root(true) . '/media/system/js/caption.js']);
unset($scripts[JUri::root(true) . '/media/jui/js/jquery.min.js']);
unset($scripts[JUri::root(true) . '/media/jui/js/jquery-noconflict.js']);
unset($scripts[JUri::root(true) . '/media/jui/js/bootstrap.min.js']);
unset($scripts[JUri::root(true) . '/media/jui/js/jquery-migrate.min.js']);

$headData['scripts'] = $scripts;
$doc->setHeadData($headData);

You need to add a prefix of JUri::root(true) before each of those file names - relative paths will not work

You can also try something like this:

$removeScripts = [
    '/media/jui/js/jquery.min.js',
    '/media/jui/js/jquery-noconflict.js',
    '/media/jui/js/jquery-migrate.min.js',
    '/media/system/js/caption.js',
];
foreach ($removeScripts as $removeScript) {
    unset($this->_scripts[JURI::root(true).$removeScript]);
}

The problem is with your in_array.

If you remove it by changing this:

foreach($doc->_scripts as $key => $script){
    if(in_array($key, $dontInclude)){
        unset($doc->_scripts[$key]);
    }
}

to this:

foreach($doc->_scripts as $key => $script){
    unset($doc->_scripts[$key]);
}

Then it works fine. It's pretty pointless checking if the array key exists as I gathered you haven't manually deleted any of these files yourself.

Hope this helps

Joomla 3.3.6 loads scripts in different way so $doc->_scripts will return nothing... so there is nothing to unset.

I recommend to use this plugin: https://github.com/Poznakomlus/joomla_options

It allows you to remove bootstrap, jQuery and mootools (you can choose what to disable).

Disclaimer: I'm not affiliated any way with plugin developer or plugin itself in any way.

If you are writing a custom template or a component, where you need to remove all the scripts loaded by default inside Joomla you can create a simple plugin and bind the execution to the onBeforeCompileHead event.

My implementation was as below. Its very simple. You can further play around with the search list, by being specific to file names or just plain blacklisting the parent folder.

protected $app;
public function onBeforeCompileHead() {
    // Front end
    if ($this->app instanceof JApplicationSite) {
        $doc = JFactory::getDocument();
        $search = array(
            'jui/js/',
            'system/js/'
        );
        foreach ($doc->_scripts as $key => $script) {
            foreach ($search as $findme) {
                if (stristr($key, $findme) !== false) {
                    unset($doc->_scripts[$key]);
                }
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top