Sunday, April 26, 2009

How to remove inclusion of MooTools javascript library in Joomla 1.5

Somethimes in projects where Joomla 1.5 is used you may need to remove inclusion of MooTools JavaScript library. It may need when your Joomla uses another JavaScript library, for example jQuery or you don't need to use functionality which provides MooTools. While looking for solution for this, I often found the following code which may be used in template code:


<?php

$headerstuff = $this->getHeadData();
$headerstuff['scripts'] = array();
$this->setHeadData($headerstuff);
?>

This code can break some plugins or module functionality because it removes all javascript inclusions which were done through Joomla API. If module or plugin includes required javascript file(s) through Joomla API and you are using code above, this code removes this inclusion and correct behaviour of plugin or module will be broken. I can offer more correct code for this purpose:


<?php

$headerstuff = $this->getHeadData();
$scripts = $headerstuff['scripts'];
$jsForRemove = array('mootools.js', 'caption.js');
foreach ($scripts as $key => $value)
foreach ($jsForRemove as $js)
if (strpos($key, $js) !== false) unset($scripts[$key]);

$headerstuff['scripts'] = $scripts;
$this->setHeadData($headerstuff);
?>

Include this code before inclusion in your template and it should work fine.



Or you can use 'Inclusions Remover' Joomla plugin which helps to remove unnecessary inclusions of scripts and CSS files which were included through Joomla API. You can download it here.

PS: If you use 'Inclusions Remover' plugin, please post a rating and a review at the Joomla! Extensions Directory.

10 comments:

  1. Thanks!! it works. So what the plugin does is remove conflict between JS file?

    I tried to remove mootols and some JCe util from page that doesnt use it. and it seems working.

    Thanks.

    ReplyDelete
  2. Don't quite get what this plugin still does it would be nice if the instructions were more detailed, with maybe some pictures showing you what it does. The Extensions page doesn't really explain and here all i see is code and no kind of breakdown of this plugins use.

    I would like to think this plugin removes unwanted JS & Css files but i don't install anything unless the developer is absolutely clear on what it does.

    ReplyDelete
  3. For some reason most of the extensions developers skip this method and rely on regular expressions to scan the whole rendered html of the page right before it is sent to browser - in onAfterRender event hanler in their system plugins.

    Why do they consider the getHeadData method unreliable - any suggestions?

    ReplyDelete
  4. To "Big Daddy Chops", provided solution is incorrect. It removes all scripts including, not only MooTools library. It causes problems with a lot of extensions.

    ReplyDelete
  5. Thank you so much for posting this! I spent 3 hours troubleshooting this problem and your solution worked perfectly.

    ReplyDelete
  6. David Walsh is Mozilla’s senior web developer, and the core developer for the MooTools
    Javascript Framework. David’s blog reflects his skills in HTML/5, JS and CSS, and offers a ton
    of engaging advice and insight into front-end technologies. Even more obvious is his passion
    for open source contribution and trial-and-error development, making his blog one of the
    most honest and engaging around.
    Website: davidwalsh.name

    ReplyDelete
  7. David Walsh is Mozilla’s senior web developer, and the core developer for the MooTools Javascript Framework. David’s blog reflects his skills in HTML/5, JS and CSS, and offers a ton of engaging advice and insight into front-end technologies. Even more obvious is his passion for open source contribution and trial-and-error development, making his blog one of the most honest and engaging around.
    Website: davidwalsh.name

    ReplyDelete
  8. David Walsh is Mozilla’s senior web developer, and the core developer for the MooTools Javascript Framework. David’s blog reflects his skills in HTML/5, JS and CSS, and offers a ton of engaging advice and insight into front-end technologies. Even more obvious is his passion for open source contribution and trial-and-error development, making his blog one of the most honest and engaging around.
    Website: davidwalsh.name

    ReplyDelete
  9. David Walsh is Mozilla’s senior web developer, and the core developer for the MooTools Javascript Framework. David’s blog reflects his skills in HTML/5, JS and CSS, and offers a ton of engaging advice and insight into front-end technologies. Even more obvious is his passion for open source contribution and trial-and-error development, making his blog one of the most honest and engaging around.
    Website: davidwalsh.name

    ReplyDelete
  10. David Walsh is Mozilla’s senior web developer, and the core developer for the MooTools Javascript Framework. David’s blog reflects his skills in HTML/5, JS and CSS, and offers a ton of engaging advice and insight into front-end technologies. Even more obvious is his passion for open source contribution and trial-and-error development, making his blog one of the most honest and engaging around.
    Website: davidwalsh.name

    ReplyDelete