Difference between revisions of "Adding JavaScript"

From Joomla! Documentation

(Remove from Cookie Jar)
(21 intermediate revisions by 7 users not shown)
Line 1: Line 1:
 +
{{version/tutor|1.5,2.5,3.1}}
 +
 
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.
  
Line 13: Line 15:
 
<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 30: Line 32:
 
<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');
 
?>
 
?>
Line 41: Line 43:
 
// Add the path parameter if the path is different than 'media/system/js/'
 
// Add the path parameter if the path is different than 'media/system/js/'
 
JHTML::script('sample.js', 'templates/custom/js/');
 
JHTML::script('sample.js', 'templates/custom/js/');
 +
?>
 +
</source>
 +
 +
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:
 +
 +
<source lang="php">
 +
<?php
 +
JHtml::script(Juri::base() . 'templates/custom/js/sample.js');
 
?>
 
?>
 
</source>
 
</source>
Line 81: Line 91:
 
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.
  
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.
+
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.
 
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.
  
=== JQuery ===
+
===Joomla 3.x jQuery ===
 +
Please see the guide on [[Javascript_Frameworks|Javascript Frameworks in Joomla 3.x]] for information about including a framework in Joomla 3.x
  
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.
+
=== Joomla 1.5/2.5 Mootools ===
  
To include the JQuery framework in your extension, you add the following code to your view.html.php or tmpl file:
+
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.
  
<source lang="php">
+
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:
JHtml::_('jquery.framework');
 
</source>
 
 
 
Using the framework method ensures JQuery is included correctly, only once, and ensures it doesn't clash with Mootools (if also loaded).
 
 
 
Then it is just a matter of leveraging the JQuery framework:
 
  
 +
FOR JOOMLA 1.5
 
<source lang="php">
 
<source lang="php">
JFactory::getDocument()->addScriptDeclaration('
+
JHTML::_('behavior.mootools');
(function($) {
 
    $(document).ready(function() {
 
        alert($("#list li").size());
 
    });
 
})(jQuery);
 
');
 
 
</source>
 
</source>
  
=== Mootools ===
+
FOR JOOMLA 2.5
 
 
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.
 
 
 
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:
 
 
 
 
<source lang="php">
 
<source lang="php">
 
JHtml::_('behavior.framework');
 
JHtml::_('behavior.framework');
 
</source>
 
</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.
+
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:
+
Then using Mootools is almost identical to jQuery:
  
 
<source lang="php">
 
<source lang="php">
 
JFactory::getDocument()->addScriptDeclaration('
 
JFactory::getDocument()->addScriptDeclaration('
window.addEvent('domready', function() {
+
window.addEvent("domready", function() {
 
     alert($("list").getElements("li").length);
 
     alert($("list").getElements("li").length);
 
});
 
});
Line 131: Line 126:
 
</source>
 
</source>
  
For more information, see [[#Further Reading]]
+
More information about Mootools is available at http://mootools.net/. For API documentation, visit http://mootools.net/docs/core.
  
 
== See Also ==
 
== See Also ==
  
[[JHtmlBehavior::framework/11.1| JHtmlBehaviour::framework method]]
+
[[JHtmlBehavior::framework/11.1| JHtmlBehaviour::framework method]] from the Joomla! Framework 11.1 documentation
  
 
[[Ajax using MooTools]]
 
[[Ajax using MooTools]]
Line 142: Line 137:
  
 
== External Links ==
 
== External Links ==
 +
http://api.joomla.org/Joomla-Platform/HTML/JHtmlBehavior.html#methodframework
  
 
http://en.wikipedia.org/wiki/JavaScript
 
http://en.wikipedia.org/wiki/JavaScript
Line 156: Line 152:
 
[[Category:Plugins]]
 
[[Category:Plugins]]
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]
 +
[[Category:JavaScript]]

Revision as of 01:36, 12 August 2013

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.

See Also[edit]

JHtmlBehaviour::framework method from the Joomla! Framework 11.1 documentation

Ajax using MooTools

Adding Javascript moo.fx to your component

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/