Hlib
Hlib

Reputation: 63

How to add the script to the top of script list in Joomla 2.5

I use $doc->addScript in top of template file to add my script (and jquery) in a head, but components are added their scripts before.

Can I control this output queue?

Upvotes: 0

Views: 867

Answers (4)

d.h.
d.h.

Reputation: 1206

You don't need to use a foreach loop for that.

For Joomla 2.5: In your templates index.php add the following if you want to prepend a script, example shows the inclusion of jquery:

$headData = $this->getHeadData();
reset($headData['scripts']);
$newHeadData = $headData['scripts'];
$jquery = array('http://code.jquery.com/jquery-1.9.1.min.js' => array('mime' => 'text/javascript', 'defer' => FALSE, 'async' => FALSE));
$newHeadData = $jquery + $newHeadData;
//$some_other_script = array("path-to-your-script" => array('mime' => 'text/javascript', 'defer' => FALSE, 'async' => FALSE));
//$newHeadData = $some_other_script + $newHeadData;
$headData['scripts'] = $newHeadData;
$this->setHeadData($headData);

For Joomla 3.3: This might work in the same way as for Joomla 2.5 as the functions getHeadData() and setHeadData() are still present. See the API documentation: http://api.joomla.org/cms-3/classes/JDocumentHTML.html

Upvotes: 1

Robert
Robert

Reputation: 789

I'm writing this for Joomla 3.3.x

Unfortunatly this cannot be done without either hacking core-files (don't do that!!) or use an own implementation for javascript-adding.

If you look into [joomlaroot]\libraries\joomla\document\document.php -> line 446, where the function addScript is defined, you will see, that the _scripts-Array in JDocument is an assoc. Array where the key is the url to the script.

If you don't want to rewrite the joomla-methods that add a script and every installed extension in your system, you should add your javascripts in the template after < jdoc:include type="head" >.

You could write a system-plugin, that uses the event "onBeforeCompileHead" to reorder the document-scripts, but i think that would be too much.

I don't know, why joomla does not simply use a sort-attribute in addScript(), that would be so needed. Also Script-Dependencies could be realised. Even Wordpress can do that.

EDIT: What you COULD do, is use JDocument::addCustomTag() to add your scripts AFTER the joomla-scripts. Because the custom-tags are rendered after all the scripts. Although this does not affect the order of scripts already loaded by other components.

Upvotes: 1

betweenbrain
betweenbrain

Reputation: 840

This can be done, but it's a bit tricky.The following code is based on https://gist.github.com/AmyStephen/5971447

$head = JFactory::getDocument()->getHeadData();
$scripts = $head['scripts'];
$newScripts = array();

foreach ($scripts as $key => $value)
{
   if (/**  test to reorder the scripts */) 
   {
        $newScripts[$key] = $value;
    }
 }            

 $head['scripts'] = $newScripts;
 JFactory::getDocument()->setHeadData($head);

In other words, the scripts are stored as an array and you need to reorder it.

Upvotes: 0

DACrosby
DACrosby

Reputation: 11430

You'll need to modify your template's index.php file. The following includes Joomla's head section:

<jdoc:include type="head"/>

Simply move $doc->addScript after this (or before it) to set which loads first. Files load top to bottom, so whatever is on top will load first.

Upvotes: 0

Related Questions