Difference between revisions of "Adding JavaScript"

From Joomla! Documentation

m (→‎Mootools: Error in quote)
m
(22 intermediate revisions by 9 users not shown)
Line 1: Line 1:
{{version/tutor|1.5,2.5,3.0}}
+
<noinclude><languages /></noinclude>
 
+
<noinclude>{{Joomla version|version=2.5 & 3.x|comment=<translate><!--T:1-->
 +
series</translate>}}</noinclude>
 +
{{-}}
 +
<translate>
 +
<!--T:2-->
 
JavaScript (also known as ECMAScript) is a scripting language primarily used in client-side web site development to extend and enhance an end-user's experience. Joomla provides developers with easy-to-use mechanisms to include JavaScript in their extensions using existing API methods.
 
JavaScript (also known as ECMAScript) is a scripting language primarily used in client-side web site development to extend and enhance an end-user's experience. Joomla provides developers with easy-to-use mechanisms to include JavaScript in their extensions using existing API methods.
 
+
</translate>
 +
<translate>
 +
<!--T:3-->
 
There are a number of methods for including JavaScript in your Joomla extensions; some of these are described below.
 
There are a number of methods for including JavaScript in your Joomla extensions; some of these are described below.
 +
</translate>
  
==Usage==
+
<translate>==Usage== <!--T:4--></translate>
  
There are three methods for embedding JavaScript into your code using the Joomla API; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script]. These methods should be called either in your component's View class (<yourcomponent>/views/<yourview>/view.html.php) or template script (<yourcomponent>/views/<yourview>/tmpl/<yourtemplate>.php or in the case of a module, in its template script (<yourmodule>/tmpl/<yourtemplate>.php).  
+
<translate>
 +
<!--T:5-->
 +
There are three methods for embedding JavaScript into your code using the Joomla API; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script]. These methods should be called either in your component's View class <tt><yourcomponent>/views/<yourview>/view.html.php</tt> or template script <tt><yourcomponent>/views/<yourview>/tmpl/<yourtemplate>.php</tt> or in the case of a module, in its template script <tt><yourmodule>/tmpl/<yourtemplate>.php</tt>.
 +
</translate>
  
===Inline JavaScript===
+
<translate>===Inline JavaScript=== <!--T:6--></translate>
  
 +
<translate>
 +
<!--T:7-->
 
Blocks of JavaScript code can be declared directly within a component or module's display template using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class' [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration addScriptDeclaration] method:
 
Blocks of JavaScript code can be declared directly within a component or module's display template using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class' [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration addScriptDeclaration] method:
 +
</translate>
  
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
$document = &JFactory::getDocument();
+
$document = JFactory::getDocument();
 
$document->addScriptDeclaration('
 
$document->addScriptDeclaration('
 
     window.event("domready", function() {
 
     window.event("domready", function() {
Line 24: Line 37:
 
</source>
 
</source>
  
===External JavaScript===
+
<translate>===External JavaScript=== <!--T:8--></translate>
  
 +
<translate>
 +
<!--T:9-->
 
Alternatively, you may wish to separate your JavaScript into a separate file. Separating your JavaScript into an external file can make your template code easier to read especially if the JavaScript is lengthy or complex.
 
Alternatively, you may wish to separate your JavaScript into a separate file. Separating your JavaScript into an external file can make your template code easier to read especially if the JavaScript is lengthy or complex.
 +
</translate>
  
 +
<translate>
 +
<!--T:10-->
 
There are two ways to include a JavaScript file using the Joomla API. The first involves using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class' [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript addScript] method:
 
There are two ways to include a JavaScript file using the Joomla API. The first involves using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class' [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript addScript] method:
 +
</translate>
  
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
$document = &JFactory::getDocument();
+
$document = JFactory::getDocument();
 
$document->addScript('/media/system/js/sample.js');
 
$document->addScript('/media/system/js/sample.js');
 
?>
 
?>
 
</source>
 
</source>
  
The second uses the [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html JHTML] class' [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] method:
+
<translate><!--T:11-->
 +
The second uses the [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html JHTML] class'[http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] method:</translate>
  
 
<source lang="php">
 
<source lang="php">
Line 46: Line 66:
 
</source>
 
</source>
  
==Description==
+
<translate>
 +
<!--T:12-->
 +
API has changed in 3.x, so the second parameter cannot be a string. If you really need to use this method, you must include the absolute link to your javacript file:
 +
</translate>
 +
 
 +
<source lang="php">
 +
<?php
 +
JHtml::script(Juri::base() . 'templates/custom/js/sample.js');
 +
?>
 +
</source>
 +
 
 +
<translate>
 +
==Description== <!--T:13-->
 
The Joomla API's [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods embed JavaScript into Joomla's index.php via the jdoc head tag:
 
The Joomla API's [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods embed JavaScript into Joomla's index.php via the jdoc head tag:
 +
</translate>
  
 
<source lang="html4strict">
 
<source lang="html4strict">
Line 53: Line 86:
 
</source>
 
</source>
  
 +
<translate>
 +
<!--T:14-->
 
Using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] or [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods to embed JavaScript includes would result in the index.php rendering the following HTML:
 
Using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] or [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods to embed JavaScript includes would result in the index.php rendering the following HTML:
 +
</translate>
  
 
<source lang="html4strict">
 
<source lang="html4strict">
Line 63: Line 99:
 
</source>
 
</source>
  
 +
<translate>
 +
<!--T:15-->
 
Calling the class method [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration] would render the following HTML:
 
Calling the class method [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration] would render the following HTML:
 +
</translate>
  
 
<source lang="html4strict">
 
<source lang="html4strict">
Line 77: Line 116:
 
</source>
 
</source>
  
 +
<translate>
 +
<!--T:16-->
 
Using these methods is highly recommended as it clearly differentiates another scripting language (JavaScript) from the main PHP code, ensures all JavaScript is correctly embedded between the <head></head> tags and, in the case of JDocument::addScript and JHTML::script ensures that a JavaScript file is included only once (I.e. there is no .js file duplication).
 
Using these methods is highly recommended as it clearly differentiates another scripting language (JavaScript) from the main PHP code, ensures all JavaScript is correctly embedded between the <head></head> tags and, in the case of JDocument::addScript and JHTML::script ensures that a JavaScript file is included only once (I.e. there is no .js file duplication).
 +
</translate>
  
== Using a JavaScript Framework ==
+
<translate>== Using a JavaScript Framework == <!--T:17--></translate>
  
 +
<translate>
 +
<!--T:18-->
 
A Javascript framework provides developers with generic functionality for handling various coding tasks in a familiar, consistent and platform-independent way. A framework enables the developer to forget about the intricacies of implementing a certain function in different web browsers and focus on the requirements of the software.
 
A Javascript framework provides developers with generic functionality for handling various coding tasks in a familiar, consistent and platform-independent way. A framework enables the developer to forget about the intricacies of implementing a certain function in different web browsers and focus on the requirements of the software.
 +
</translate>
  
Two Javascript frameworks are provided as part of Joomla 3.0; JQuery and Mootools. JQuery is a newly introduced framework which integrates with Joomla's new Bootstrap HTML framework; Mootools is Joomla's legacy Javascript library which is now superseded by JQuery and is included for backwards compatibility with 3rd party extensions.
+
<translate>
 +
<!--T:19-->
 +
Two [[S:MyLanguage/Javascript Frameworks|Javascript Frameworks]] are provided as part of Joomla 3.x; jQuery and Mootools. jQuery is a newly introduced framework which integrates with Joomla's new Bootstrap HTML framework; Mootools is Joomla's legacy Javascript library which is now superseded by jQuery and is included for backwards compatibility with 3rd party extensions.
 +
</translate>
  
 +
<translate>
 +
<!--T:20-->
 
In nearly all cases you should use a framework when developing Javascript in your extensions or templates and including one is very simple with Joomla's API.
 
In nearly all cases you should use a framework when developing Javascript in your extensions or templates and including one is very simple with Joomla's API.
 +
</translate>
  
=== JQuery ===
+
<translate>
 +
===Joomla 3.x jQuery === <!--T:21-->
 +
Please see the guide on [[Javascript_Frameworks|Javascript Frameworks in Joomla 3.x]] for information about including a framework in Joomla 3.x
 +
</translate>
  
As of Joomla 3.0 it is highly recommended you use the JQuery framework for all your Javascript as it is loaded by nearly every template which uses the Bootstrap HTML/CSS framework. In those few instances where JQuery is not available, you can easily include the framework using the JHTMLJQuery class' framework method.
+
<translate>
 +
=== Joomla 1.5/2.5 Mootools === <!--T:22-->
 +
Unless you are maintaining Javascript code which leverages Mootools or you are developing an extension for Joomla 2.5 or earlier it is recommended you use jQuery instead.
 +
</translate>
  
To include the JQuery framework in your extension, you add the following code to your view.html.php or tmpl file:
+
<translate>
 +
<!--T:23-->
 +
Firstly, you will need to include the Mootools code in your extension. To include the Mootools framework in your extension, you add the following code to your view.html.php or tmpl file:
 +
</translate>
 +
 
 +
<translate><!--T:24-->
 +
FOR JOOMLA 1.5</translate>
 +
<source lang="php">
 +
JHTML::_('behavior.mootools');
 +
</source>
  
 +
<translate><!--T:25-->
 +
FOR JOOMLA 2.5</translate>
 
<source lang="php">
 
<source lang="php">
JHtml::_('jquery.framework');
+
JHtml::_('behavior.framework');
 
</source>
 
</source>
  
Using the framework method ensures JQuery is included correctly, only once, and ensures it doesn't clash with Mootools (if also loaded).
+
<translate>
 +
<!--T:26-->
 +
The above code results in the same outcome as the similar jQuery framework statement; that is it ensures Mootools is included correctly and only once.
 +
</translate>
  
Then it is just a matter of leveraging the JQuery framework:
+
<translate><!--T:27-->
 +
Then using Mootools is almost identical to jQuery:</translate>
  
 
<source lang="php">
 
<source lang="php">
 
JFactory::getDocument()->addScriptDeclaration('
 
JFactory::getDocument()->addScriptDeclaration('
(function($) {
+
window.addEvent("domready", function() {
     $(document).ready(function() {
+
     alert($("list").getElements("li").length);
        alert($("#list li").size());
+
});
    });
 
})(jQuery);
 
 
');
 
');
 
</source>
 
</source>
  
More information about JQuery is available at http://docs.jquery.com/How_jQuery_Works. For API documentation, visit http://docs.jquery.com/.
+
<translate>
 +
<!--T:28-->
 +
More information about Mootools is available at http://mootools.net/. For API documentation, visit http://mootools.net/docs/core.
 +
</translate>
  
=== Mootools ===
+
<translate>== Important notes for 3rd party developers == <!--T:29--></translate>
  
Unless you are maintaining Javascript code which leverages Mootools or you are developing an extension for a Joomla version prior to 3.0 it is recommended you use JQuery instead.
+
<translate>
 +
<!--T:30-->
 +
If you are creating a custom template override or extension that needs to add a custom JS file, make sure to add important dependencies such as Jquery or Mootools before your custom JS files. JS framework fiels must always be  loaded before any other files to make sure they get executed first, otherwise other files that load before the frameworks they need are likely to end in JS exceptions.
 +
</translate>
  
Firstly, you will need to include the Mootools code in your extension. To include the Mootools framework in your extension, you add the following code to your view.html.php or tmpl file:
+
<translate><!--T:31-->
 +
Some templates like Protostar or Beez insert all the dependencies you need using functions like</translate>
 +
 
 +
JHtml::_('bootstrap.framework');
 +
 
 +
<translate>
 +
<!--T:32-->
 +
to load Jquery + Bootstrap, but you should not rely in this fact on your extensions or custom templates overrides. Always make sure your extension or custom override load the dependencies you need before the template does it, I will explain why later:
 +
</translate>
 +
 
 +
<translate>
 +
<!--T:33-->
 +
For example if you got a custom template override that needs to insert a JS file with some Jquery scripts that does fancy things on all the pages where that template override is being used. In that case you should declare this on the top section of that override file:
 +
</translate>
 +
 
 +
JHtml::_('jquery.framework');
 +
$doc->addScript('templates/'.$this->template.'/js/fancy-script.js');
 +
 
 +
<translate><!--T:34-->
 +
If you are developing a 3rd party extension that you plan to put on the Joomla! extension directory you should do something like this:</translate>
 +
 
 +
if($params->get('add_extension_resources', false))
 +
{
 +
    JHtml::_('jquery.framework');
 +
    $doc->addScript('media/com_fancy/js/fancy-script.js');
 +
}
 +
 
 +
<translate>
 +
<!--T:35-->
 +
The conditional clause to decide whether to add or not the extension resources is '''strongly encouraged''' and considered a '''good practice''' because it gives flexibility to 3rd party developers who don't want to use your extension resources and use custom/modified files without having to battle with Joomla! using workarounds and hacks to be able to remove your original extensions resources in order to avoid duplicates and conflicts.
 +
</translate>
 +
 
 +
<translate>
 +
==== Explanation ==== <!--T:36-->
 +
 
 +
<!--T:37-->
 +
If you check the source code of the index.php from the Protostar template, you can see that the statements
 +
</translate>
 +
 
 +
JHtml::_('bootstrap.framework');
 +
 
 +
<translate><!--T:38-->
 +
is added way before the statement</translate>
 +
 
 +
<jdoc:include type="head" />
 +
 
 +
<translate><!--T:39-->
 +
this can make you think that the framework files and your 3rd party files using methods like</translate>
  
<source lang="php">
+
$doc->addScript('templates/'.$this->template.'/js/fancy-script.js');
JHtml::_('behavior.framework');
+
$doc->addScript('media/com_fancy/js/fancy-script.js');
</source>
 
  
The above code results in the same outcome as the similar JQuery framework statement; that is it ensures Mootools is included correctly and only once.
+
<translate>
 +
<!--T:40-->
 +
will be added in the right order at the right spot, '''but that is not the case''', because extension files and template override files are processed '''first''' and the index.php file of your current template is processed the '''last'''. This will cause that your custom JS files get inserted first and the framework files inserted from the template get inserted after.
 +
</translate>
  
Then using Mootools is almost identical to JQuery:
+
<translate>
 +
<!--T:41-->
 +
This happens because the Joomla API (such as $doc->addScript) uses an array to store the JS files paths and they get rendered in the document in the same order they where inserted into that array (FIFO stack), also once a file path is inserted on the array and another API call tries to insert the same file this action is ignored to avid duplicates, it also means that the order of the files is not altered when the same files is attempted to be inserted several times.
 +
</translate>
  
<source lang="php">
+
<translate><!--T:42-->
JFactory::getDocument()->addScriptDeclaration('
+
Having said that doing this</translate>  
window.addEvent("domready", function() {
 
    alert($("list").getElements("li").length);
 
});
 
');
 
</source>
 
  
More information about Mootools is available at http://mootools.net/. For API documentation, visit http://mootools.net/docs/core.
+
JHtml::_('jquery.framework');
 +
$doc->addScript('templates/'.$this->template.'/js/fancy-script.js');
  
== See Also ==
+
<translate><!--T:43-->
 +
at your custom templates overrides and extension, is '''required''' and does not cause harm or conflict with the call</translate>
  
[[JHtmlBehavior::framework/11.1| JHtmlBehaviour::framework method]] from the Joomla! Framework 11.1 documentation
+
JHtml::_('bootstrap.framework');
  
[[Ajax using MooTools]]
+
<translate><!--T:44-->
 +
at your template index.php file.</translate>
  
[[Adding Javascript moo.fx to your component]]
+
<translate>== External Links == <!--T:45--></translate>
  
== External Links ==
 
 
http://api.joomla.org/Joomla-Platform/HTML/JHtmlBehavior.html#methodframework
 
http://api.joomla.org/Joomla-Platform/HTML/JHtmlBehavior.html#methodframework
  
Line 156: Line 281:
 
http://jquery.com/
 
http://jquery.com/
  
 +
<noinclude>
 +
<translate>
 +
<!--T:46-->
 
[[Category:Templates]]
 
[[Category:Templates]]
 
[[Category:Modules]]
 
[[Category:Modules]]
Line 161: Line 289:
 
[[Category:Plugins]]
 
[[Category:Plugins]]
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]
 +
[[Category:JavaScript]]
 +
</translate>
 +
</noinclude>

Revision as of 08:32, 27 May 2015

Other languages:
Deutsch • ‎English • ‎Nederlands • ‎eesti • ‎español • ‎français • ‎português do Brasil • ‎български • ‎فارسی • ‎हिन्दी • ‎বাংলা • ‎中文(台灣)‎
Joomla! 
2.5 & 3.x
series

JavaScript (also known as ECMAScript) is a scripting language primarily used in client-side web site development to extend and enhance an end-user's experience. Joomla provides developers with easy-to-use mechanisms to include JavaScript in their extensions using existing API methods. There are a number of methods for including JavaScript in your Joomla extensions; some of these are described below.

Usage[edit]

There are three methods for embedding JavaScript into your code using the Joomla API; JDocument::addScriptDeclaration, JDocument::addScript and script. These methods should be called either in your component's View class <yourcomponent>/views/<yourview>/view.html.php or template script <yourcomponent>/views/<yourview>/tmpl/<yourtemplate>.php or in the case of a module, in its template script <yourmodule>/tmpl/<yourtemplate>.php.

Inline JavaScript[edit]

Blocks of JavaScript code can be declared directly within a component or module's display template using the JDocument class' addScriptDeclaration method:

<?php
$document = JFactory::getDocument();
$document->addScriptDeclaration('
    window.event("domready", function() {
        alert("An inline JavaScript Declaration");
    });
');
?>

External JavaScript[edit]

Alternatively, you may wish to separate your JavaScript into a separate file. Separating your JavaScript into an external file can make your template code easier to read especially if the JavaScript is lengthy or complex.

There are two ways to include a JavaScript file using the Joomla API. The first involves using the JDocument class' addScript method:

<?php
$document = JFactory::getDocument();
$document->addScript('/media/system/js/sample.js');
?>

The second uses the JHTML class'script method:

<?php
// Add the path parameter if the path is different than 'media/system/js/'
JHTML::script('sample.js', 'templates/custom/js/');
?>

API has changed in 3.x, so the second parameter cannot be a string. If you really need to use this method, you must include the absolute link to your javacript file:

<?php
JHtml::script(Juri::base() . 'templates/custom/js/sample.js');
?>

Description[edit]

The Joomla API's JDocument::addScriptDeclaration, JDocument::addScript and script methods embed JavaScript into Joomla's index.php via the jdoc head tag:

<jdoc:include type="head"/>

Using the JDocument::addScript or script methods to embed JavaScript includes would result in the index.php rendering the following HTML:

<head>
...
<script type="text/javaScript" src="/media/system/js/sample.js"></script>
...
</head>

Calling the class method JDocument::addScriptDeclaration would render the following HTML:

<head>
...
<script type="text/javaScript">
window.addEvent("domready", function() {
    alert("Embedded block of JS here");
});
</script>
...
</head>

Using these methods is highly recommended as it clearly differentiates another scripting language (JavaScript) from the main PHP code, ensures all JavaScript is correctly embedded between the <head></head> tags and, in the case of JDocument::addScript and JHTML::script ensures that a JavaScript file is included only once (I.e. there is no .js file duplication).

Using a JavaScript Framework[edit]

A Javascript framework provides developers with generic functionality for handling various coding tasks in a familiar, consistent and platform-independent way. A framework enables the developer to forget about the intricacies of implementing a certain function in different web browsers and focus on the requirements of the software.

Two Javascript Frameworks are provided as part of Joomla 3.x; jQuery and Mootools. jQuery is a newly introduced framework which integrates with Joomla's new Bootstrap HTML framework; Mootools is Joomla's legacy Javascript library which is now superseded by jQuery and is included for backwards compatibility with 3rd party extensions.

In nearly all cases you should use a framework when developing Javascript in your extensions or templates and including one is very simple with Joomla's API.

Joomla 3.x jQuery[edit]

Please see the guide on Javascript Frameworks in Joomla 3.x for information about including a framework in Joomla 3.x

Joomla 1.5/2.5 Mootools[edit]

Unless you are maintaining Javascript code which leverages Mootools or you are developing an extension for Joomla 2.5 or earlier it is recommended you use jQuery instead.

Firstly, you will need to include the Mootools code in your extension. To include the Mootools framework in your extension, you add the following code to your view.html.php or tmpl file:

FOR JOOMLA 1.5

JHTML::_('behavior.mootools');

FOR JOOMLA 2.5

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

The above code results in the same outcome as the similar jQuery framework statement; that is it ensures Mootools is included correctly and only once.

Then using Mootools is almost identical to jQuery:

JFactory::getDocument()->addScriptDeclaration('
window.addEvent("domready", function() {
    alert($("list").getElements("li").length);
});
');

More information about Mootools is available at http://mootools.net/. For API documentation, visit http://mootools.net/docs/core.

Important notes for 3rd party developers[edit]

If you are creating a custom template override or extension that needs to add a custom JS file, make sure to add important dependencies such as Jquery or Mootools before your custom JS files. JS framework fiels must always be loaded before any other files to make sure they get executed first, otherwise other files that load before the frameworks they need are likely to end in JS exceptions.

Some templates like Protostar or Beez insert all the dependencies you need using functions like

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

to load Jquery + Bootstrap, but you should not rely in this fact on your extensions or custom templates overrides. Always make sure your extension or custom override load the dependencies you need before the template does it, I will explain why later:

For example if you got a custom template override that needs to insert a JS file with some Jquery scripts that does fancy things on all the pages where that template override is being used. In that case you should declare this on the top section of that override file:

JHtml::_('jquery.framework');
$doc->addScript('templates/'.$this->template.'/js/fancy-script.js');

If you are developing a 3rd party extension that you plan to put on the Joomla! extension directory you should do something like this:

if($params->get('add_extension_resources', false))
{
    JHtml::_('jquery.framework');
    $doc->addScript('media/com_fancy/js/fancy-script.js');
}

The conditional clause to decide whether to add or not the extension resources is strongly encouraged and considered a good practice because it gives flexibility to 3rd party developers who don't want to use your extension resources and use custom/modified files without having to battle with Joomla! using workarounds and hacks to be able to remove your original extensions resources in order to avoid duplicates and conflicts.

Explanation[edit]

If you check the source code of the index.php from the Protostar template, you can see that the statements

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

is added way before the statement

<jdoc:include type="head" />

this can make you think that the framework files and your 3rd party files using methods like

$doc->addScript('templates/'.$this->template.'/js/fancy-script.js');
$doc->addScript('media/com_fancy/js/fancy-script.js');

will be added in the right order at the right spot, but that is not the case, because extension files and template override files are processed first and the index.php file of your current template is processed the last. This will cause that your custom JS files get inserted first and the framework files inserted from the template get inserted after.

This happens because the Joomla API (such as $doc->addScript) uses an array to store the JS files paths and they get rendered in the document in the same order they where inserted into that array (FIFO stack), also once a file path is inserted on the array and another API call tries to insert the same file this action is ignored to avid duplicates, it also means that the order of the files is not altered when the same files is attempted to be inserted several times.

Having said that doing this

JHtml::_('jquery.framework');
$doc->addScript('templates/'.$this->template.'/js/fancy-script.js');

at your custom templates overrides and extension, is required and does not cause harm or conflict with the call

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

at your template index.php file.

External Links[edit]

http://api.joomla.org/Joomla-Platform/HTML/JHtmlBehavior.html#methodframework

http://en.wikipedia.org/wiki/JavaScript

http://www.w3schools.com/js/default.asp

http://mootools.net/

http://jquery.com/