Loading jQuery before other Javascript in Joomla 3
Asked Answered
A

4

8

I'm adapting a custom component to Joomla 3. It relies on jQuery, which I previously loaded in myself. Now that it's included in the base template, I don't need to. However, my custom javascript that relies on jQuery is being loaded first. I load them using the following form:

$document = JFactory::getDocument();
$document->addScript( PATH TO SCRIPT);

This properly includes them in the <head> element, but they are loaded before jQuery.

Some quick searching reveals abstract class JHtmlJquery in libraries/cms/html/jquery.php but I'm unsure of from where this is called.

How can I change the load order so that jQuery is loaded before my scripts that depend upon it? And can I do this without getting into the core code?

Airlift answered 25/10, 2012 at 22:36 Comment(1)
use $document->addCustomTag('<script src="PATH TO SCRIPT" type="text/javascript"></script>'); custom tags will be loaded after all other scripts loaded.Bromoform
N
6

I found a solution. I didn't want both the template as well as the components to load bootstrap, plus on pages that are just single articles that load some modules, I didn't want to have each module load bootstrap as well.. (I mean this is getting a bit ridiculous..) So I decided to take bootstrap out of the template completely and I built a system plugin that just loads bootstrap and then I set the loading order of the plugin to load first. Works great..

// Bootstrap Loader


    jimport('joomla.plugin.plugin');

    class plgSystemBootstrapLoader extends JPlugin {

        function onAfterRoute() {

            // Load Bootstrap
            JHtml::_('bootstrap.framework');
            $document = JFactory::getDocument();
            $document->addStyleSheet('/media/jui/css/bootstrap.min.css');
            $document->addStyleSheet('/media/jui/css/bootstrap-responsive.css');
        }

    }
Nereus answered 8/4, 2013 at 20:30 Comment(1)
This is probably the best solution. It's elegant and follows the Joomla paradigm.Airlift
A
10

Mario's answer is going in the right direction, but it doesn't quite do it. It turns out, you must include the code for JHtml::('bootstrap.framework'); within your component (not just the template files) before loading additional scripts. I was using the stock J3 template, protostar, which includes the twitter bootstrap in its template index.php.

The code for my custom view.html.php is now as follows:

class MyView extends JViewLegacy
{
    function display($tpl = null)
    {
        $document = JFactory::getDocument();
        JHtml::_('bootstrap.framework');

        $document->addScript( PATH_TO_SCRIPT );

        ...
    }
}

This behaves as expected and the jQuery and Bootstrap files (jquery.min.js, jquery-noconflict.js, and bootstrap.min.js) are included in the <head> before my custom scripts.

Airlift answered 29/10, 2012 at 16:2 Comment(0)
N
6

I found a solution. I didn't want both the template as well as the components to load bootstrap, plus on pages that are just single articles that load some modules, I didn't want to have each module load bootstrap as well.. (I mean this is getting a bit ridiculous..) So I decided to take bootstrap out of the template completely and I built a system plugin that just loads bootstrap and then I set the loading order of the plugin to load first. Works great..

// Bootstrap Loader


    jimport('joomla.plugin.plugin');

    class plgSystemBootstrapLoader extends JPlugin {

        function onAfterRoute() {

            // Load Bootstrap
            JHtml::_('bootstrap.framework');
            $document = JFactory::getDocument();
            $document->addStyleSheet('/media/jui/css/bootstrap.min.css');
            $document->addStyleSheet('/media/jui/css/bootstrap-responsive.css');
        }

    }
Nereus answered 8/4, 2013 at 20:30 Comment(1)
This is probably the best solution. It's elegant and follows the Joomla paradigm.Airlift
G
2

What template are you using? In J3 it's a common sense to load the jQuery platform at the bottom of the page, so that the pages load faster. Look in your template for JHtml::_('bootstrap.framework'); // Loads the jQuery js scripts and try to load your js after this.

Gwennie answered 26/10, 2012 at 0:39 Comment(1)
It is not so good, for example K2 (the most used CCK for Joomla) and a lot of components require jQuery loaded "before".Mucronate
A
1

Joomla probably writes the scripts and css files included in the $document->addScript( PATH TO SCRIPT); manner in the order they are added by executing component and module code.

I've searched a little but couldn't find a way to influence the loading of seperate declarations or jQuery. But I can offer an other solution.

I can't say I'm certain this will be THE solution to your problem, because I haven't got a clue as to what your code does, or when it needs to do something. So I'm coming in with the broadest fix possible ;)

This will work, as long as your code won't have to be active before page ready.

Wrap your custom code in a js on ready statement, so it won't do anything until the page ( and consequently jQuery has been loaded ).

if (document.readyState === "complete") { 
    DoYourStuff(); 
}
Amadoamador answered 26/10, 2012 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.