Introducing FLEXIcontent labs - Yireo

Conditional load of jQuery

Sunday, 19 June 2011

jQuery is cool - everybody knows it. It's able to run together with MooTools (used by Joomla!) and even ProtoType (used by Magento) and is - with that - known for its ability to bypass JavaScript conflicts. But unfortunately, loading jQuery twice also throws errors. YOOtheme came up with a solution for this, and we are happy to tell that we adapted this method for all our own extensions as well.

Loading jQuery only once

In their blog, the YOOtheme team comes up with a simple solution: Every developer that uses the jQuery framework in their own extension should notify other extensions that jQuery is already loaded. A simple flag determines whether jQuery is loaded or not. So, if an extension loads jQuery it sets the flag:

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

Ofcourse all extensions also need to check whether the flag is set, before actually loading jQuery. This conditional statement looks like the following:

if(JFactory::getApplication()->get('jquery') == false) {
    // @todo: load jQuery
    JFactory::getApplication()->set('jquery', true);

Ofcourse this does not solve the problem when different extensions require different versions of jQuery, but it's a good start nonetheless. We have implemented this method in all new versions of our own Yireo extensions, among which MageBridge, Vm2Mage, SimpleLists and Fancybox.

 

About Yireo

Yireo tries to help webdevelopers build successful Joomla! and Magento sites.

More about Yireo