Remove Mootools From Joomla Header


Joomla 1.5 is definitely a better CMS if you compare it to Joomla 1.0. One of few complaints I have heard and personally experienced with Joomla 1.5 is the inclusion of Mootools (and caption.js) by default if you use Joomla template method to display header information in your template. What’s the problem, one may ask? Well, the problem is that the Mootools file that comes with Joomla 1.5 is whopping 74k in size. From developer’s point of view, I may never need to use mootools in my application. And from end user point of view, this really slows down initial page load times especially for users who have slower connections, and you never get a second chance to make a first impression.Mootools is definitely a very useful JavaScript framework. (And since the perfectionist founder can even FIRE his developer, that also shows it has certain standard.) But loading it without using it really irritates some people.So why the hell on earth would Joomla 1.5 includes Mootools? The reason is because Joomla 1.5 Admin section uses Mootools. It’s the C (Controller) in the MVC (Model-View-Controller) that needs it. If you have installed the latest version and seen the AJAX effects from the admin area, you may probably know what I am talking about.Now the question is how to remove the Mootools and other JavaScript files from the header when the end users see it, and keep it exists when a administrator sees it? Don’t worry, here is the solution, below is the code I use to remove the mootools.js and caption.js

<?php
$user =& JFactory::getUser();
if ($user->get('guest') == 1) {
$headerstuff = $this->getHeadData();
$headerstuff['scripts'] = array();
$this->setHeadData($headerstuff); }
?>
<jdoc:include type="head" />

In case you want to keep the caption.js, (some Joomla! components, modules or plugins uses caption.js, for example:Acajoom), you can choose to remove mootools only. Below is the PHP code i use to do so:

<?php
// Remove auto generated mootool from header
$headerstuff = $this->getHeadData();
reset($headerstuff['scripts']);
$moo = key($headerstuff['scripts']);
unset($headerstuff['scripts'][$moo]);
$this->setHeadData($headerstuff);
?>
<jdoc:include type="head">

Hope these tips help!

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Technorati
  • Twitter
  • Yahoo! Bookmarks

  1. #1 by Simeon on March 13, 2008 - 3:45 pm

    Could you tell, where this code shoudl be placed?

  2. #2 by admin on March 14, 2008 - 7:06 am

    Hi, Simeon

    It should be placed within the header (<head></ head>) of your template index file (index.php).

    For instance, if you use the beez template that comes with Joomla, just navigate from Joomla root to templates/beez, under the beez folder. find the index.php file, open it up, just place the code within the header.

  3. #3 by John on April 17, 2008 - 8:46 am

    Wow! Thank you.

    I recomend this code if you are having conflicts with lightbox.js and mootools.js.

    I replaced the with the code you supplied and it worked perfectly!

    Lightbox now works as it should.

    Thanks again!

  4. #4 by admin on April 28, 2008 - 9:25 am

    No problem, John! very happy to know that your problem’s solved!

  5. #5 by Tom on May 7, 2008 - 12:04 am

    Hi,
    I always get the following error when placing your code in the header of my index.php:

    Parse error: syntax error, unexpected ‘;’, expecting T_PAAMAYIM_NEKUDOTAYIM in /WWWROOT/…/htdocs/templates/rhuk_milkyway/index.php on line 21

    What do I have to change?

  6. #6 by admin on May 7, 2008 - 4:24 am

    Hi, Tom

    Did you choose the first chunck of code from the above? Sorry, I found a typo, can you try copy paste the code again? Let me know if the problem persists.

  7. #7 by Tom on May 8, 2008 - 7:33 am

    Hi,

    actually I had the problems with the *second* chunk of code (the one to delete mootools only from the header). But I’ve inserted the first code now and that seems to work nicely.

    I hope I won’t need caption.js in the future …

    Thanks!

  8. #8 by admin on May 9, 2008 - 3:53 am

    Hi, Tom

    Glad to know that. :)

  9. #9 by Steffi on July 7, 2008 - 5:03 am

    Thx for the code! But do i see it right that disabling it with your code works only for unregistered guests? So it’s no solution for using Lightbox, as for editors or publishers want to see the images in a lightbox, and also want to edit their submissions.

  10. #10 by admin on July 7, 2008 - 10:49 am

    Hi, Steffi. that’s a good point, maybe you can try adjust the third line: $user->get(‘guest’) to something more suitable to your needs.

  11. #11 by Steffi on July 12, 2008 - 1:19 am

    Well, i could change it so that it works also for registered users. but if i change it up to editors or publishers, they will see the lightboxes in the pictures, but they are no longer able to edit their submissions. so there is no solution for this issue if authors want to see the lightbox AND want to edit their submissions :(

  12. #12 by admin on July 15, 2008 - 4:32 am

    sorry, Steffi

    I am a bit confused, if you need the javascript files all the time, why do you want to remove it in the first place?

    how exactly do you want your applcation to work?

  13. #13 by unikyu on July 17, 2008 - 6:55 am

    Hi,

    Big Thank you for this article. I am new to CMS’s and Joomla. Started constructing a site in April and have been pulling out my hair trying to speed it up ever since. Finally figured mootools had something to do with it and voila… have now discovered your article.

    I wonder if you could write the exact syntax for removing mootools for both guests and registered users alike… You suggested that the code be modified to suit a user’s need, but I am such a novice, I am not sure how to include both guests and registered users and exclude admin.

    I hope I have made my request clear. Thanks again.

  14. #14 by admin on July 18, 2008 - 5:09 am

    Hi, unikyu

    I think the following is what you need:

    <?php
    $user =& JFactory::getUser();
    if ($user->get('guest') == 1 || $user->get('usertype') == 'Registered') {
    $headerstuff = $this->getHeadData();
    $headerstuff['scripts'] = array();
    $this->setHeadData($headerstuff); }
    ?>

    hope it helps! :)

  15. #15 by unikyu on July 19, 2008 - 4:04 am

    Again, I thank you. I’ve tried both codes and the joomla performance grader tells me that mootools was removed and that the site should be running faster. However, it is difficult for me to know if site performance is in fact improving (as it should without such a massive file), because the pages continue to open inconsistently (sometimes fast and at other times so slow to the point that they time out).

    I have come to find out, on the day I tried this ‘remove mootools’ code, that the hosting company is aware that there is some problem with their server’s handling of database requests. They say they will upgrade in 2 weeks.

    Will wait till then to give you proper feedback on the effects of this code… Right now, site performance continues to be grrrr! *smile*

    But thanks for providing this option.

  16. #16 by teino on August 8, 2008 - 3:33 am

    1. getHeadData();
    4. reset($headerstuff['scripts']);
    5. $moo = key($headerstuff['scripts']);
    6. unset($headerstuff['scripts'][$moo]);
    7. $this->setHeadData($headerstuff);
    8. ?>
    9.

    I have a error message

    Class is not defined
    caption.js (line 22)
    var JCaption = new Class({

  17. #17 by Agent007 on August 23, 2008 - 6:19 pm

    Hi

    Thanks for the very useful information. I tried using the second code block (the remove mootools only part) with an “if block” for guests and registered users based on what you recommended to uikyu. Here is the code:

    get(‘guest’) == 1) || $user->get(‘usertype’) == ‘Registered’) {
    $headerstuff = $this->getHeadData();
    reset($headerstuff['scripts']);
    $moo = key($headerstuff['scripts']);
    unset($headerstuff['scripts'][$moo]);
    $this->setHeadData($headerstuff);
    } //end if
    ?>

    However, where as it works fine for guests, it doesn’t for registered users. The situation remained the same even when the “if block” was removed.

    Incidentally, your first code block (for the removal of caption.js and mootools worked fine for guests and registered users.

    Any ideas? I really want to remove only mootools for solely guests and users from the ‘Registered’ group.

    Regards

  18. #18 by Agent007 on August 23, 2008 - 6:24 pm

    Sorry, I just noticed that in removing my comments from the code, I inadvertently removed part of the structure was removed as well. Here is the complete block once again:

    get(‘guest’) == 1) || $user->get(‘usertype’) == ‘Registered’) {
    $headerstuff = $this->getHeadData();
    reset($headerstuff['scripts']);
    $moo = key($headerstuff['scripts']);
    unset($headerstuff['scripts'][$moo]);
    $this->setHeadData($headerstuff);
    } //end if
    ?>

  19. #19 by Agent007 on August 23, 2008 - 6:26 pm

    Hmmmmm it was not an error on my part after all for some odd reason, some of the code gets cut off.

  20. #20 by admin on August 24, 2008 - 2:47 am

    Hi, Agent007

    Is the script working fine for you now?

    you might try to use the ‘view plain’ when copy and paste my code to your text editor, or type it out without copy and paste.

  21. #21 by admin on August 24, 2008 - 2:48 am

    Hi, teino

    where did you see the error message?

  22. #22 by Andre on August 24, 2008 - 4:42 am

    Regarding Teino’s message:
    “I have a error message
    Class is not defined
    caption.js (line 22)
    var JCaption = new Class({

    I get that too, in Firefox’s Firebug. The error happens in media/system/js/caption.js, using the second piece of code, which removes mootools only.

    Speeds up loading time though!

  23. #23 by admin on August 24, 2008 - 8:00 am

    For now, try add this line after the second piece of code: <script type=”text/javascript” src=”< ?php echo $this->baseurl ?>/media/system/js/caption.js”></script>

    I will find out a better fix when I am free. :)

  24. #24 by Andre on August 24, 2008 - 3:14 pm

    Nah. That gives me 2 firebug errors, class not defined.
    I’ve inserted the first piece of code in the header, no errors there and it does not seem to affect anything on the site so I’ll stick with that.

    Thanks for the efforts mate!

  25. #25 by Francesco on September 1, 2008 - 11:17 am

    No one of yours solution help me..cause when i try to insert your code in IE i see half page black..and the other part with no position….

    I don’t understand :(

    Help me :(

  26. #26 by fmj on September 7, 2008 - 3:33 am

    It seems to work even for registered users adding the same piece of code after

    get(‘guest’) == 1 || $user->get(‘usertype’) == ‘Registered’) {
    $headerstuff = $this->getHeadData();
    reset($headerstuff['scripts']);
    $moo = key($headerstuff['scripts']);
    unset($headerstuff['scripts'][$moo]);
    $this->setHeadData($headerstuff);
    }
    ?>

  27. #27 by fmj on September 8, 2008 - 2:05 am

    after

  28. #28 by fmj on September 8, 2008 - 2:05 am

    after

  29. #29 by fmj on September 8, 2008 - 2:06 am

    after jdoc:include type=”head”

  30. #30 by Brent Kerr on September 8, 2008 - 4:09 am

    Hey, thanks for the script.

    Just thought you should know; I see “=&” on line 2 of the first chunk of code instead of “=&” which obviously gives an error when copied directly. Perhaps your blog software is ‘cleaning it up’?

    Cheers,
    Brent.

  31. #31 by Brent Kerr on September 8, 2008 - 4:11 am

    Aaahh, my post gave an example of the problem – how ironic. What I meant to say was:
    I see “= & a m p ;” (the HTML code for character ‘&’) on line 2 of the first chunk of code instead of “=&”.

  32. #32 by admin on September 8, 2008 - 6:39 am

    Hi, Brent Kerr

    thanks for pointing it out! :)

    I believe it’s because a recent wordpress update, it now renders HTML differently. Guess I need to read through my old posts to fix this kind of problem.

  33. #33 by seralex on September 24, 2008 - 2:37 pm

    thanks a lot! From Russia with love!

  34. #34 by rigo on October 7, 2008 - 11:41 am

    Does your code, pretty much take out any head code. I am using the plugin Toheader to put javascript code into the header of certain articles and for some reason it is not appearing after inserting your code. So I guess the question is does your code stop all header code form being generated except head code in the template files? I hope that makes sense
    THankis

  35. #35 by admin on October 9, 2008 - 10:28 am

    Hi, rigo

    I am planning to write a plugin to make the header script rendering more flexible.

  36. #36 by Jay on October 15, 2008 - 1:52 am

    Thanks very much for this – just saved even more head banging against a wall whilst using this awful CMS :)

  37. #37 by jalil on November 24, 2008 - 7:34 pm

    hi,

    very nice work.

    finally we can breathe easier without worrying about
    cows roaming about on our sites.

    i just wish to note that the remark you made about
    “the Mootools file that comes with Joomla 1.5 is whopping 74k in size. ” isn’t quite accurate.

    It isn’t slow because of its size and in fact it is small relatively speaking ( i have used javascripts 3 times the size without performance issues ), but it is the way the moos work that degrades the performance so. the cows is just too fat in processes.

    u know what i mean if you compare the backends of 1.0 and 1.5. but of course it looks ever so sweet at the front end, i stay with my opinion 1.0 is better.

    have spoken my mind. :)

    ta.

  38. #38 by Реклама. Комплекс on January 2, 2009 - 2:14 pm

    I will use this method of exclude .js in my projects
    http://reklama-komplex.kiev.ua

  39. #39 by Dylan on January 13, 2009 - 2:01 pm

    Hoi,
    Kunt u mij ook vertellen of ik met uw scritp mijn probleem met mootools en
    IE kan oplossen? Bezoekers van mjin website die Internet Explorer gebruiken,
    crashen bij het oproepen van de website. IE geeft dan een error mee en de gebruiker moet IE opnieuw opstarten. Daarna geeft IE geen foutmelding weer.

    Gavick (joomla) geeft aan dat het een mootools probleem kan zijn, maar met zekerheid kunnen ze dat niet zeggen. Weet u misschien met uw ervaring
    hoe ik dit kan oplossen, want merendeels van de bezoekers gebruiken IE, was het maar waar dat iedereen Firofox ging gebruiken. :)

    Bedankt alvast! vr gr Dylan

  40. #40 by Dylan on January 13, 2009 - 2:26 pm

    Hi,

    I was wondering since i have these mootools problems on my website http://www.bkml.nl if this script works for my problems with Internet Explorer.

    Visitors of my website gets an IE error while they are typing the URL in the browser. After clicking the error away IE restarts and the visitor has to retype the address in the addressbalk again.

    Could it be an mootools problem, and if yes can i use this script into my index.php. I have tried already but i only get syntax errors. :(
    I;m a newby into joomla so i need some advanced help around the internet.

    Kind regards Dylan

    ps. it only crashes with IE.

  41. #41 by alex on January 21, 2009 - 1:37 pm

    great my friend…
    thank you a lot

  42. #42 by Matt on January 23, 2009 - 4:19 pm

    Hi. This is really useful! I’d like to be able to load mootools on a particular page though. Is there a way to do that?

  43. #43 by jaruzek on January 24, 2009 - 2:35 pm

    This looks like a great solution for removing MooTools! Is it pretty rock solid now? I plan on using this method, then relinking MooTools and jQuery using Google’s AJAX Libraries API.

    Have you worked out a Joomla plugin for this yet? It’s be cool if you could work in the Google AJAX Libraries.

    Thanks,
    jaruzek

  44. #44 by Gary on January 26, 2009 - 9:31 am

    This does nothing for me. Its in the head section but the frontend code still calls mootools.js :(

    Any ideas?

    Thanks

    Gary

  45. #45 by Drewid on February 13, 2009 - 8:34 am

    Life saver, spent half a day working out that the mootools is conflicting with prototype and scriptaculous. Time for a celebratory coffee!

    cheers

  46. #46 by Adam @ Unreleased Games on March 5, 2009 - 10:06 am

    Thanks for these tips, I’m going to give this a go as I’m having load speed problems at the moment. I suspect mootools could be helping it to go slow.

  47. #47 by dwi on March 26, 2009 - 7:31 am

    Gimana sih caranya memasukkan kode java script ke Joomla, kalo pake insert HTML gak bisa!!!!

  48. #48 by Joomla Template Creator on May 10, 2009 - 7:04 pm

    Joomla is a top notch CMS and this post is a perfect example why. Thank you for this information.

  49. #49 by drifter on May 10, 2009 - 9:11 pm

    Hi

    Just a few hours ago I was saying to my friend that these moo cows are taking their time to get into my browser.

    Now I do a search to check if you have released a Joomla! 1.5 version of Multithumb and voila… You solve both problems. Congrats on the new family member. Now if I can just find the donation button… It must be here somewhere…

    thanks

    dM

  50. #50 by admin on May 11, 2009 - 8:53 pm

    Glad it helps!

(will not be published)