Feb

16

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!



Similar Posts

Comments

Name (required)

Email (required)

Website

Speak your mind

37 Comments so far

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

    Could you tell, where this code shoudl be placed?

  2. 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. 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. admin on April 28, 2008 9:25 am

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

  5. 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. 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. 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. admin on May 9, 2008 3:53 am

    Hi, Tom

    Glad to know that. :)

  9. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. admin on August 24, 2008 2:48 am

    Hi, teino

    where did you see the error message?

  22. 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. 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. 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. 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. 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. fmj on September 8, 2008 2:05 am

    after

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

    after

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

    after jdoc:include type=”head”

  30. 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. 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. 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. seralex on September 24, 2008 2:37 pm

    thanks a lot! From Russia with love!

  34. 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. 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. 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. 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.

Sponsors




Links