ContextSwitch
ContextSwitch

Reputation: 2837

How do I stop Joomla from including jQuery?

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?

Upvotes: 1

Views: 11159

Answers (8)

Alaksandar Jesus Gene
Alaksandar Jesus Gene

Reputation: 6887

This worked for me in joomla 3.9

<?php

defined('_JEXEC') or die('Restricted access');
$unset_scripts = [
    '/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 ($unset_scripts as $script) {
    unset($this->_scripts[JURI::root(true) . $script]);
}
if (isset($this->_script['text/javascript'])) {
    $captionJsStr = '%jQuery\(window\)\.on\(\'load\',\s*function\(\)\s*{\s*new\s*JCaption\(\'img.caption\'\);\s*}\);\s*%';
    $this->_script['text/javascript'] = preg_replace($captionJsStr, '', $this->_script['text/javascript']);
    if (empty($this->_script['text/javascript'])) {
        unset($this->_script['text/javascript']);
    }

}
$this->_scripts = array();

?>
<!DOCTYPE html>

Upvotes: 0

Mohd Abdul Mujib
Mohd Abdul Mujib

Reputation: 13948

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

Upvotes: 0

Don Cranford
Don Cranford

Reputation: 21

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

Upvotes: 1

David Taiaroa
David Taiaroa

Reputation: 25495

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);

Upvotes: 5

Sulibrat
Sulibrat

Reputation: 151

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.

Upvotes: 0

ContextSwitch
ContextSwitch

Reputation: 2837

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.

Upvotes: 1

Lodder
Lodder

Reputation: 19743

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

Upvotes: 0

George Wilson
George Wilson

Reputation: 5705

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

Upvotes: 2

Related Questions