How to remove unwanted javascript files from joomla page.

Share this post on:

In this short tutorial I will show you how you can remove the idle javascript files from the joomla template page, which you don’t want to use. I have described here some ways, you can choose to use the convenient one to you. Use these scripts right before the closing </head> tag.

First Code:

<?php
// this code will stop the loading of all the default joomla js files.
$this->_script = $this->_scripts = array(); 
?>

Second Code:

<?php
         // here you will get the array which contain all the major scripts
         $document = JFactory::getDocument(); 
         $headData = $document->getHeadData();
         $scripts = $headData['scripts'];

         // here you can remove the script you want.
         unset($scripts['/media/system/js/mootools-core.js']);
         unset($scripts['/media/system/js/caption.js']);
         $headData['scripts'] = $scripts;
         $document->setHeadData($headData);
?>

Third Code:

<?php
// unset a single js file
unset($this->_scripts['/media/system/js/mootools-core.js']);
?>

Forth code:

<?php
// Another way to disable unwanted js.
$user =&amp; JFactory::getUser();
if($user->get('guest') == 1 || $user->get('guest') != 1) {
    $search = array('mootools', 'caption.js');
    // remove the js files
    foreach($this->_scripts as $key => $script) {
        foreach($search as $findme) {
            if(stristr($key, $findme) !== false) {
                unset($this->_scripts[$key]);

            }

        }

    }

}

?>

Hope, it will help someone.

thanks.

Author: Nohman Habib

I basically work in the CMS, like Joomla and WordPress and in framework like Laravel and have keen interest in developing mobile apps by utilizing hybrid technology. I also have experience working in AWS technology. In terms of CMS, I give Joomla the most value because I found it so much user freindly and my clients feel so much easy to manage their project in it.

View all posts by Nohman Habib >

3 Comments

  1. Hi, the article is helpful, but I have a question that I want to hide the unwanted javascript from some pages but on other I want to show these, as I have some modules which need these javascript files to run properly.

    1. A typical url for a page in joomla is like index.php?option=com_example&view=abc&id=3&Itemid=12. Now if you want to display the javascript on selected pages only, you can get the value of view, id or Itemid as per your requirement by $itemid = JRequest::getVar('Itemid'); and apply if statement with it.

Leave a Reply

Your email address will not be published. Required fields are marked *

PHP Code Snippets Powered By : XYZScripts.com