The Joomla! 3.0 core ships with jQuery, and previous Joomla! versions did not. Extension developers like us therefor included a custom call to load jQuery in the past, whenever it was needed. However this standard needs updating. Here's our proposal.

Loading jQuery only once

Because jQuery could be loaded in the past more than 1 time (and loading jQuery twice leads to unwanted errors, plus it makes no sense to load it twice), extension developers more or less agreed to use a flag in the Joomla! application-class:

$loaded = JFactory::getApplication()->get('jquery');

If the flag was not yet loaded, it could simply be loaded by your own extension, after which the flag needed to be set for other extensions to pick up on:

JFactory::getApplication()->set('jquery', true);

Joomla! 3.0 changes this

The Joomla! 3.0 core changes the JApplication-class in such a way that the get() and set() methods can no longer be used - the class no longer extends the JObject class which provided these methods. Simply put: Using the code above under Joomla! 3.0 throws a PHP Fatal Error.

But Joomla! 3.0 has also introduced jQuery as a core feature. Third party extensions no longer require to load jQuery, because it is already shipped with Joomla!. The only thing that extensions require is a simple call to the JHtml-class:

JHtml::_('jquery.framework');

Code proposal

So what is our proposal? Simple: Use the old method for Joomla! 2.5 and older, and use the new method for Joomla! 3.0. The version of Joomla! can be checked using framework-calls as well, so the code would look a bit like this:

JLoader::import( 'joomla.version' );
$version = new JVersion();
if (version_compare( $version->RELEASE, '2.5', '<=')) {
    if(JFactory::getApplication()->get('jquery') !== true) {
        // load jQuery here
        JFactory::getApplication()->set('jquery', true);
    }
} else {
    JHtml::_('jquery.framework');
}

We will be including this code in MageBridge and our other Joomla! extensions.

Posted on October 22, 2012

About the author

Author Jisse Reitsma

Jisse Reitsma is the founder of Yireo, extension developer, developer trainer and 3x Magento Master. His passion is for technology and open source. And he loves talking as well.

Sponsor Yireo

Looking for a training in-house?

Let's get to it!

We don't write too commercial stuff, we focus on the technology (which we love) and we regularly come up with innovative solutions. Via our newsletter, you can keep yourself up to date on all of this coolness. Subscribing only takes seconds.

Do not miss out on what we say

This will be the most interesting spam you have ever read

We don't write too commercial stuff, we focus on the technology (which we love) and we regularly come up with innovative solutions. Via our newsletter, you can keep yourself up to date on all of this coolness. Subscribing only takes seconds.