2

I would like to minify a js file of a Joomla module. For example, the file:

joomla_site/modules/mod_module/tmpl/js/file.js

which has been copied to following location:

joomla_site/templates/my_template/html/mod_module/tmpl/js/file.js

It's been minified. I reloaded my site, and there was no change. The site is still using the un-minified js file. What am I missing?

3 Answers 3

1

Assuming the module is using the following method to import the script:

JHtml::_('script', 'path/to/file.js', array(), true);

Not using $doc->script()

Then you need to copy the file to the following path:

joomla-site/templates/my_template/html/mod_module/file.js

The path shouldn't have tmpl/js/ in it.

1

If it's your own custom module - go with another answer solution, but if it's not yours - I suggest you to go with JCH Optimizer - free version fully covers your needs.

3
  • JCH Optimize is for caching, not overriding JS files
    – Lodder
    May 23, 2016 at 9:18
  • 1
    JCH Optimize is for caching, minifying and compiling to 1 file all CSS and all JS to another. This man wants to minify js - so it's his option, believe me. Used it at least 20 times already.
    – Alexandr
    May 23, 2016 at 9:22
  • 2
    @Alexandr: I would suggest to complete your answer by adding this type of comment in your answer - explaining why you suggest JCH optimize and what can be achieved by using it.
    – FFrewin
    Jun 22, 2016 at 9:28
1

You can also do this by including Javascript file in footer so that it will overwrite the file.

  1. Create a new file called footer.php in /libraries/joomla/document/html/renderer/ directory:

  2. In /libraries/joomla/document/document.php add after

    public $_scripts = array();
    this
    public $_footer_scripts = array();

  3. Add method addFooterScript() after the addScript() method

    function addFooterScript($url, $type = "text/javascript", $defer = false, $async = false) {
        $this->_footer_scripts[$url]['mime'] = $type;
        $this->_footer_scripts[$url]['defer'] = $defer;
        $this->_footer_scripts[$url]['async'] = $async;
        return $this;
    }
    
  4. In index.php in you template folder add just before the tag
    <jdoc:include type="footer" />

  5. Done!
    Now in layouts inside your template's folder you can use the following code to add javascript files to footer:

    $doc =& JFactory::getDocument();
    $doc->addFooterScript('JavaScript_File_Path.js');
    

For more instructions check this - Javascript files includes in footer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.