<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://docs.sandbox.joomla.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Chivitli</id>
	<title>Joomla! Documentation - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://docs.sandbox.joomla.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Chivitli"/>
	<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/Special:Contributions/Chivitli"/>
	<updated>2026-05-15T09:34:47Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.0</generator>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J3.x:Adding_JavaScript_and_CSS_to_the_page&amp;diff=102239</id>
		<title>J3.x:Adding JavaScript and CSS to the page</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J3.x:Adding_JavaScript_and_CSS_to_the_page&amp;diff=102239"/>
		<updated>2013-08-12T06:57:17Z</updated>

		<summary type="html">&lt;p&gt;Chivitli: /* Inserting from within a PHP file */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{version/tutor|2.5,3.x}}{{review||Code is no longer 2.5/3.x applicable}}&lt;br /&gt;
&lt;br /&gt;
== Inserting from a File ==&lt;br /&gt;
To have a well-formed XHTML document, you must put all references to Javascript and CSS files within the &amp;lt;code&amp;gt;&amp;lt;head&amp;gt;&amp;lt;/code&amp;gt; portion. Since Joomla! generates all the HTML that makes up a page before output, it is possible to add these references within the &amp;lt;head&amp;gt; tags from your extension. The simplest way to do this is to make use of the functionality built in to Joomla!. In Joomla 2.5 you can still use the following, but it&#039;s marked as deprecated and changed in 3.x:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Add a reference to a Javascript file&lt;br /&gt;
// The default path is &#039;media/system/js/&#039;&lt;br /&gt;
JHtml::script($filename, $path, $mootools);&lt;br /&gt;
&lt;br /&gt;
// Add a reference to a CSS file&lt;br /&gt;
// The default path is &#039;media/system/css/&#039;&lt;br /&gt;
JHtml::stylesheet($filename, $path);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
API has changed in 3.x, so the second parameter cannot be a string. If you really need to use these methods, you must include the absolute link to your files:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
JHtml::script(Juri::base() . &#039;templates/custom/js/sample.js&#039;, $mootools);&lt;br /&gt;
JHtml::stylesheet(Juri::base() . &#039;templates/custom/css/style.js&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using these functions, Joomla! will take care of any additional requirements. For example, if your Javascript requires Mootools, setting &amp;lt;code&amp;gt;$mootools = true&amp;lt;/code&amp;gt; will automatically ensure that Mootools is loaded, if it has not already been done.&lt;br /&gt;
&lt;br /&gt;
However, the above functions will not be flexible enough for every scenario, and so it is possible to tap into the underlying functionality instead. Of course, you will also need to manually code some of the steps that would be done automatically using the functions above.&lt;br /&gt;
&lt;br /&gt;
First, get a reference to the current document object:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$document = JFactory::getDocument();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
For a stylesheet, use this code:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$document-&amp;gt;addStyleSheet($url);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To add a Javascript file, use this code:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$document-&amp;gt;addScript($url);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Note this will **NOT** include Mootools or jQuery. If your script requires Mootools or jQuery see [[Javascript_Frameworks]] for full details on how to include them (note jQuery can only be included natively on Joomla 3.0+).&lt;br /&gt;
&lt;br /&gt;
== Inserting from within a PHP file ==&lt;br /&gt;
&lt;br /&gt;
If your Javascript or CSS are generated using PHP, you can add the script or stylesheet directly into the head of your document:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$document = JFactory::getDocument();&lt;br /&gt;
&lt;br /&gt;
// Add Javascript&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
    window.event(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
        alert(&amp;quot;An inline JavaScript Declaration&amp;quot;);&lt;br /&gt;
    });&lt;br /&gt;
&#039;);&lt;br /&gt;
&lt;br /&gt;
// Add styles&lt;br /&gt;
$style = &#039;body {&#039;&lt;br /&gt;
        . &#039;background: #00ff00;&#039;&lt;br /&gt;
        . &#039;color: rgb(0,0,255);&#039;&lt;br /&gt;
        . &#039;}&#039;; &lt;br /&gt;
$document-&amp;gt;addStyleDeclaration($style);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Javascript Examples ==&lt;br /&gt;
&lt;br /&gt;
For example, the following code is used to define a custom tool tip that takes advantage of mootools.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
function getToolTipJS($toolTipVarName, $toolTipClassName){&lt;br /&gt;
    $javascript = &#039;window.addEvent(\&amp;quot;domready\&amp;quot;, function(){&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t&amp;quot;  .&#039;var $toolTipVarName = new Tips($$(&amp;quot;&#039; . $toolTipVarName .&#039;&amp;quot;), {&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;className: &amp;quot;&#039; .$toolTipClassName .&#039;&amp;quot;,&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;initialize: function(){&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t\t&amp;quot;    .&#039;this.fx = new Fx.Style(this.toolTip, &amp;quot;opacity&amp;quot;, {duration: 500, wait: false}).set(0);&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;},&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;onShow: function(toolTip){&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t\t&amp;quot;    .&#039;this.fx.start(1);&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;},&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;onHide: function(toolTip) {&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t\t&amp;quot;    .&#039;this.fx.start(0);&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;}&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t&amp;quot;  .&#039;});&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &#039;});&#039; .&amp;quot;\n\n&amp;quot;;&lt;br /&gt;
    return $javascript;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$document = JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addStyleSheet(&amp;quot;/joomla/components/com_mycustomcomponent/css/mytooltip.css&amp;quot;,&#039;text/css&#039;,&amp;quot;screen&amp;quot;);&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration(getToolTipJS(&amp;quot;mytool&amp;quot;,&amp;quot;MyToolTip&amp;quot;));&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Note that in order for this Javascript to be functionally useful, it would be necessary to include the appropriate class name in the HTML, as well as providing the &amp;lt;code&amp;gt;mytooltip.css&amp;lt;/code&amp;gt; file. Both are outside the scope of this article.&lt;br /&gt;
&lt;br /&gt;
== CSS Examples ==&lt;br /&gt;
&lt;br /&gt;
This is also useful if your inserting a parameter/form field of CSS into your code. For example in a module, you might want a user to choose to call the colour of the border. After calling the parameter/form field and assigning it a variable $bordercolor in mod_example.php. Then in tmpl/default.php you can include the following&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$document = JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addStyleSheet(JURI::base() . &#039;modules/mod_example/css/mod_example.css&#039;);&lt;br /&gt;
$style = &#039;#example {&lt;br /&gt;
	border-color:#&#039; . $bordercolor . &#039;;&lt;br /&gt;
	}&#039;;&lt;br /&gt;
$document-&amp;gt;addStyleDeclaration( $style );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here mod_example.css contains the CSS file of any non-parameter based styles. Then the bordercolor parameter/form field is added in separately&lt;br /&gt;
&lt;br /&gt;
==Add Custom Tag==&lt;br /&gt;
&lt;br /&gt;
There will be some occasions where even these functions are not flexible enough, as they are limited to writing the contents of &amp;lt;code&amp;gt;&amp;lt;script /&amp;gt;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;&amp;lt;style /&amp;gt;&amp;lt;/code&amp;gt; tags, and cannot add anything outside those tags. One example would be the inclusion of a stylesheet link within conditional comments, so that it is picked up only by Internet Explorer 6 and earlier. To do this, use &amp;lt;code&amp;gt;$document-&amp;gt;addCustomTag&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$stylelink = &#039;&amp;lt;!--[if lte IE 6]&amp;gt;&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
$stylelink .= &#039;&amp;lt;link rel=&amp;quot;stylesheet&amp;quot; href=&amp;quot;../css/IEonly.css&amp;quot; /&amp;gt;&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
$stylelink .= &#039;&amp;lt;![endif]--&amp;gt;&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$document = JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addCustomTag($stylelink);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]] [[Category:Tutorials]][[Category:Component Development]][[Category:Module Development]]&lt;br /&gt;
[[Category:JavaScript]]&lt;br /&gt;
[[Category:CSS]]&lt;br /&gt;
&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>Chivitli</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J3.x:Adding_JavaScript_and_CSS_to_the_page&amp;diff=102238</id>
		<title>J3.x:Adding JavaScript and CSS to the page</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J3.x:Adding_JavaScript_and_CSS_to_the_page&amp;diff=102238"/>
		<updated>2013-08-12T06:56:43Z</updated>

		<summary type="html">&lt;p&gt;Chivitli: /* Inserting from within a PHP file */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{version/tutor|2.5,3.x}}{{review||Code is no longer 2.5/3.x applicable}}&lt;br /&gt;
&lt;br /&gt;
== Inserting from a File ==&lt;br /&gt;
To have a well-formed XHTML document, you must put all references to Javascript and CSS files within the &amp;lt;code&amp;gt;&amp;lt;head&amp;gt;&amp;lt;/code&amp;gt; portion. Since Joomla! generates all the HTML that makes up a page before output, it is possible to add these references within the &amp;lt;head&amp;gt; tags from your extension. The simplest way to do this is to make use of the functionality built in to Joomla!. In Joomla 2.5 you can still use the following, but it&#039;s marked as deprecated and changed in 3.x:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Add a reference to a Javascript file&lt;br /&gt;
// The default path is &#039;media/system/js/&#039;&lt;br /&gt;
JHtml::script($filename, $path, $mootools);&lt;br /&gt;
&lt;br /&gt;
// Add a reference to a CSS file&lt;br /&gt;
// The default path is &#039;media/system/css/&#039;&lt;br /&gt;
JHtml::stylesheet($filename, $path);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
API has changed in 3.x, so the second parameter cannot be a string. If you really need to use these methods, you must include the absolute link to your files:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
JHtml::script(Juri::base() . &#039;templates/custom/js/sample.js&#039;, $mootools);&lt;br /&gt;
JHtml::stylesheet(Juri::base() . &#039;templates/custom/css/style.js&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using these functions, Joomla! will take care of any additional requirements. For example, if your Javascript requires Mootools, setting &amp;lt;code&amp;gt;$mootools = true&amp;lt;/code&amp;gt; will automatically ensure that Mootools is loaded, if it has not already been done.&lt;br /&gt;
&lt;br /&gt;
However, the above functions will not be flexible enough for every scenario, and so it is possible to tap into the underlying functionality instead. Of course, you will also need to manually code some of the steps that would be done automatically using the functions above.&lt;br /&gt;
&lt;br /&gt;
First, get a reference to the current document object:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$document = JFactory::getDocument();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
For a stylesheet, use this code:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$document-&amp;gt;addStyleSheet($url);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To add a Javascript file, use this code:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$document-&amp;gt;addScript($url);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Note this will **NOT** include Mootools or jQuery. If your script requires Mootools or jQuery see [[Javascript_Frameworks]] for full details on how to include them (note jQuery can only be included natively on Joomla 3.0+).&lt;br /&gt;
&lt;br /&gt;
== Inserting from within a PHP file ==&lt;br /&gt;
&lt;br /&gt;
If your Javascript or CSS are generated using PHP, you can add the script or stylesheet directly into the head of your document:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$document = JFactory::getDocument();&lt;br /&gt;
// Add Javascript&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
    window.event(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
        alert(&amp;quot;An inline JavaScript Declaration&amp;quot;);&lt;br /&gt;
    });&lt;br /&gt;
&#039;);&lt;br /&gt;
&lt;br /&gt;
// Add styles&lt;br /&gt;
$style = &#039;body {&#039;&lt;br /&gt;
        . &#039;background: #00ff00;&#039;&lt;br /&gt;
        . &#039;color: rgb(0,0,255);&#039;&lt;br /&gt;
        . &#039;}&#039;; &lt;br /&gt;
$doc-&amp;gt;addStyleDeclaration($style);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Javascript Examples ==&lt;br /&gt;
&lt;br /&gt;
For example, the following code is used to define a custom tool tip that takes advantage of mootools.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
function getToolTipJS($toolTipVarName, $toolTipClassName){&lt;br /&gt;
    $javascript = &#039;window.addEvent(\&amp;quot;domready\&amp;quot;, function(){&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t&amp;quot;  .&#039;var $toolTipVarName = new Tips($$(&amp;quot;&#039; . $toolTipVarName .&#039;&amp;quot;), {&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;className: &amp;quot;&#039; .$toolTipClassName .&#039;&amp;quot;,&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;initialize: function(){&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t\t&amp;quot;    .&#039;this.fx = new Fx.Style(this.toolTip, &amp;quot;opacity&amp;quot;, {duration: 500, wait: false}).set(0);&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;},&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;onShow: function(toolTip){&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t\t&amp;quot;    .&#039;this.fx.start(1);&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;},&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;onHide: function(toolTip) {&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t\t&amp;quot;    .&#039;this.fx.start(0);&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;}&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t&amp;quot;  .&#039;});&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &#039;});&#039; .&amp;quot;\n\n&amp;quot;;&lt;br /&gt;
    return $javascript;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$document = JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addStyleSheet(&amp;quot;/joomla/components/com_mycustomcomponent/css/mytooltip.css&amp;quot;,&#039;text/css&#039;,&amp;quot;screen&amp;quot;);&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration(getToolTipJS(&amp;quot;mytool&amp;quot;,&amp;quot;MyToolTip&amp;quot;));&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Note that in order for this Javascript to be functionally useful, it would be necessary to include the appropriate class name in the HTML, as well as providing the &amp;lt;code&amp;gt;mytooltip.css&amp;lt;/code&amp;gt; file. Both are outside the scope of this article.&lt;br /&gt;
&lt;br /&gt;
== CSS Examples ==&lt;br /&gt;
&lt;br /&gt;
This is also useful if your inserting a parameter/form field of CSS into your code. For example in a module, you might want a user to choose to call the colour of the border. After calling the parameter/form field and assigning it a variable $bordercolor in mod_example.php. Then in tmpl/default.php you can include the following&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$document = JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addStyleSheet(JURI::base() . &#039;modules/mod_example/css/mod_example.css&#039;);&lt;br /&gt;
$style = &#039;#example {&lt;br /&gt;
	border-color:#&#039; . $bordercolor . &#039;;&lt;br /&gt;
	}&#039;;&lt;br /&gt;
$document-&amp;gt;addStyleDeclaration( $style );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here mod_example.css contains the CSS file of any non-parameter based styles. Then the bordercolor parameter/form field is added in separately&lt;br /&gt;
&lt;br /&gt;
==Add Custom Tag==&lt;br /&gt;
&lt;br /&gt;
There will be some occasions where even these functions are not flexible enough, as they are limited to writing the contents of &amp;lt;code&amp;gt;&amp;lt;script /&amp;gt;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;&amp;lt;style /&amp;gt;&amp;lt;/code&amp;gt; tags, and cannot add anything outside those tags. One example would be the inclusion of a stylesheet link within conditional comments, so that it is picked up only by Internet Explorer 6 and earlier. To do this, use &amp;lt;code&amp;gt;$document-&amp;gt;addCustomTag&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$stylelink = &#039;&amp;lt;!--[if lte IE 6]&amp;gt;&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
$stylelink .= &#039;&amp;lt;link rel=&amp;quot;stylesheet&amp;quot; href=&amp;quot;../css/IEonly.css&amp;quot; /&amp;gt;&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
$stylelink .= &#039;&amp;lt;![endif]--&amp;gt;&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$document = JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addCustomTag($stylelink);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]] [[Category:Tutorials]][[Category:Component Development]][[Category:Module Development]]&lt;br /&gt;
[[Category:JavaScript]]&lt;br /&gt;
[[Category:CSS]]&lt;br /&gt;
&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>Chivitli</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J3.x:Adding_JavaScript_and_CSS_to_the_page&amp;diff=102237</id>
		<title>J3.x:Adding JavaScript and CSS to the page</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J3.x:Adding_JavaScript_and_CSS_to_the_page&amp;diff=102237"/>
		<updated>2013-08-12T06:49:19Z</updated>

		<summary type="html">&lt;p&gt;Chivitli: /* Inserting from a File */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{version/tutor|2.5,3.x}}{{review||Code is no longer 2.5/3.x applicable}}&lt;br /&gt;
&lt;br /&gt;
== Inserting from a File ==&lt;br /&gt;
To have a well-formed XHTML document, you must put all references to Javascript and CSS files within the &amp;lt;code&amp;gt;&amp;lt;head&amp;gt;&amp;lt;/code&amp;gt; portion. Since Joomla! generates all the HTML that makes up a page before output, it is possible to add these references within the &amp;lt;head&amp;gt; tags from your extension. The simplest way to do this is to make use of the functionality built in to Joomla!. In Joomla 2.5 you can still use the following, but it&#039;s marked as deprecated and changed in 3.x:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Add a reference to a Javascript file&lt;br /&gt;
// The default path is &#039;media/system/js/&#039;&lt;br /&gt;
JHtml::script($filename, $path, $mootools);&lt;br /&gt;
&lt;br /&gt;
// Add a reference to a CSS file&lt;br /&gt;
// The default path is &#039;media/system/css/&#039;&lt;br /&gt;
JHtml::stylesheet($filename, $path);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
API has changed in 3.x, so the second parameter cannot be a string. If you really need to use these methods, you must include the absolute link to your files:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
JHtml::script(Juri::base() . &#039;templates/custom/js/sample.js&#039;, $mootools);&lt;br /&gt;
JHtml::stylesheet(Juri::base() . &#039;templates/custom/css/style.js&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using these functions, Joomla! will take care of any additional requirements. For example, if your Javascript requires Mootools, setting &amp;lt;code&amp;gt;$mootools = true&amp;lt;/code&amp;gt; will automatically ensure that Mootools is loaded, if it has not already been done.&lt;br /&gt;
&lt;br /&gt;
However, the above functions will not be flexible enough for every scenario, and so it is possible to tap into the underlying functionality instead. Of course, you will also need to manually code some of the steps that would be done automatically using the functions above.&lt;br /&gt;
&lt;br /&gt;
First, get a reference to the current document object:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$document = JFactory::getDocument();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
For a stylesheet, use this code:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$document-&amp;gt;addStyleSheet($url);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To add a Javascript file, use this code:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$document-&amp;gt;addScript($url);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Note this will **NOT** include Mootools or jQuery. If your script requires Mootools or jQuery see [[Javascript_Frameworks]] for full details on how to include them (note jQuery can only be included natively on Joomla 3.0+).&lt;br /&gt;
&lt;br /&gt;
== Inserting from within a PHP file ==&lt;br /&gt;
&lt;br /&gt;
However, your Javascript or CSS might not be located in a separate file&amp;amp;mdash;you might want to generate them using PHP. In this case you can write the script or stylesheet directly into the head of your document:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$document = JFactory::getDocument();&lt;br /&gt;
// Add Javascript&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration($javascript, $type);&lt;br /&gt;
&lt;br /&gt;
// Add styles&lt;br /&gt;
$document-&amp;gt;addStyleDeclaration($styles, $type);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Javascript Examples ==&lt;br /&gt;
&lt;br /&gt;
For example, the following code is used to define a custom tool tip that takes advantage of mootools.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
function getToolTipJS($toolTipVarName, $toolTipClassName){&lt;br /&gt;
    $javascript = &#039;window.addEvent(\&amp;quot;domready\&amp;quot;, function(){&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t&amp;quot;  .&#039;var $toolTipVarName = new Tips($$(&amp;quot;&#039; . $toolTipVarName .&#039;&amp;quot;), {&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;className: &amp;quot;&#039; .$toolTipClassName .&#039;&amp;quot;,&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;initialize: function(){&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t\t&amp;quot;    .&#039;this.fx = new Fx.Style(this.toolTip, &amp;quot;opacity&amp;quot;, {duration: 500, wait: false}).set(0);&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;},&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;onShow: function(toolTip){&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t\t&amp;quot;    .&#039;this.fx.start(1);&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;},&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;onHide: function(toolTip) {&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t\t&amp;quot;    .&#039;this.fx.start(0);&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;}&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t&amp;quot;  .&#039;});&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &#039;});&#039; .&amp;quot;\n\n&amp;quot;;&lt;br /&gt;
    return $javascript;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$document = JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addStyleSheet(&amp;quot;/joomla/components/com_mycustomcomponent/css/mytooltip.css&amp;quot;,&#039;text/css&#039;,&amp;quot;screen&amp;quot;);&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration(getToolTipJS(&amp;quot;mytool&amp;quot;,&amp;quot;MyToolTip&amp;quot;));&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Note that in order for this Javascript to be functionally useful, it would be necessary to include the appropriate class name in the HTML, as well as providing the &amp;lt;code&amp;gt;mytooltip.css&amp;lt;/code&amp;gt; file. Both are outside the scope of this article.&lt;br /&gt;
&lt;br /&gt;
== CSS Examples ==&lt;br /&gt;
&lt;br /&gt;
This is also useful if your inserting a parameter/form field of CSS into your code. For example in a module, you might want a user to choose to call the colour of the border. After calling the parameter/form field and assigning it a variable $bordercolor in mod_example.php. Then in tmpl/default.php you can include the following&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$document = JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addStyleSheet(JURI::base() . &#039;modules/mod_example/css/mod_example.css&#039;);&lt;br /&gt;
$style = &#039;#example {&lt;br /&gt;
	border-color:#&#039; . $bordercolor . &#039;;&lt;br /&gt;
	}&#039;;&lt;br /&gt;
$document-&amp;gt;addStyleDeclaration( $style );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here mod_example.css contains the CSS file of any non-parameter based styles. Then the bordercolor parameter/form field is added in separately&lt;br /&gt;
&lt;br /&gt;
==Add Custom Tag==&lt;br /&gt;
&lt;br /&gt;
There will be some occasions where even these functions are not flexible enough, as they are limited to writing the contents of &amp;lt;code&amp;gt;&amp;lt;script /&amp;gt;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;&amp;lt;style /&amp;gt;&amp;lt;/code&amp;gt; tags, and cannot add anything outside those tags. One example would be the inclusion of a stylesheet link within conditional comments, so that it is picked up only by Internet Explorer 6 and earlier. To do this, use &amp;lt;code&amp;gt;$document-&amp;gt;addCustomTag&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$stylelink = &#039;&amp;lt;!--[if lte IE 6]&amp;gt;&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
$stylelink .= &#039;&amp;lt;link rel=&amp;quot;stylesheet&amp;quot; href=&amp;quot;../css/IEonly.css&amp;quot; /&amp;gt;&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
$stylelink .= &#039;&amp;lt;![endif]--&amp;gt;&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$document = JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addCustomTag($stylelink);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]] [[Category:Tutorials]][[Category:Component Development]][[Category:Module Development]]&lt;br /&gt;
[[Category:JavaScript]]&lt;br /&gt;
[[Category:CSS]]&lt;br /&gt;
&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>Chivitli</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J3.x:Adding_JavaScript_and_CSS_to_the_page&amp;diff=102236</id>
		<title>J3.x:Adding JavaScript and CSS to the page</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J3.x:Adding_JavaScript_and_CSS_to_the_page&amp;diff=102236"/>
		<updated>2013-08-12T06:48:04Z</updated>

		<summary type="html">&lt;p&gt;Chivitli: /* Inserting from a File */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{version/tutor|2.5,3.x}}{{review||Code is no longer 2.5/3.x applicable}}&lt;br /&gt;
&lt;br /&gt;
== Inserting from a File ==&lt;br /&gt;
To have a well-formed XHTML document, you must put all references to Javascript and CSS files within the &amp;lt;code&amp;gt;&amp;lt;head&amp;gt;&amp;lt;/code&amp;gt; portion. Since Joomla! generates all the HTML that makes up a page before output, it is possible to add these references within the &amp;lt;head&amp;gt; tags from your extension. The simplest way to do this is to make use of the functionality built in to Joomla!. In Joomla 2.5 you can still use the following, but it&#039;s marked as deprecated and changed in 3.x:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Add a reference to a Javascript file&lt;br /&gt;
// The default path is &#039;media/system/js/&#039;&lt;br /&gt;
JHtml::script($filename, $path, $mootools);&lt;br /&gt;
&lt;br /&gt;
// Add a reference to a CSS file&lt;br /&gt;
// The default path is &#039;media/system/css/&#039;&lt;br /&gt;
JHtml::stylesheet($filename, $path);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Using these functions, Joomla! will take care of any additional requirements. For example, if your Javascript requires Mootools, setting &amp;lt;code&amp;gt;$mootools = true&amp;lt;/code&amp;gt; will automatically ensure that Mootools is loaded, if it has not already been done.&lt;br /&gt;
&lt;br /&gt;
API has changed in 3.x, so the second parameter cannot be a string. If you really need to use these methods, you must include the absolute link to your files:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
JHtml::script(Juri::base() . &#039;templates/custom/js/sample.js&#039;, $mootools);&lt;br /&gt;
JHtml::stylesheet(Juri::base() . &#039;templates/custom/css/style.js&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, the above functions will not be flexible enough for every scenario, and so it is possible to tap into the underlying functionality instead. Of course, you will also need to manually code some of the steps that would be done automatically using the functions above.&lt;br /&gt;
&lt;br /&gt;
First, get a reference to the current document object:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$document = JFactory::getDocument();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
For a stylesheet, use this code:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$document-&amp;gt;addStyleSheet($url);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To add a Javascript file, use this code:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$document-&amp;gt;addScript($url);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Note this will **NOT** include Mootools or jQuery. If your script requires Mootools or jQuery see [[Javascript_Frameworks]] for full details on how to include them (note jQuery can only be included natively on Joomla 3.0+).&lt;br /&gt;
&lt;br /&gt;
== Inserting from within a PHP file ==&lt;br /&gt;
&lt;br /&gt;
However, your Javascript or CSS might not be located in a separate file&amp;amp;mdash;you might want to generate them using PHP. In this case you can write the script or stylesheet directly into the head of your document:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$document = JFactory::getDocument();&lt;br /&gt;
// Add Javascript&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration($javascript, $type);&lt;br /&gt;
&lt;br /&gt;
// Add styles&lt;br /&gt;
$document-&amp;gt;addStyleDeclaration($styles, $type);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Javascript Examples ==&lt;br /&gt;
&lt;br /&gt;
For example, the following code is used to define a custom tool tip that takes advantage of mootools.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
function getToolTipJS($toolTipVarName, $toolTipClassName){&lt;br /&gt;
    $javascript = &#039;window.addEvent(\&amp;quot;domready\&amp;quot;, function(){&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t&amp;quot;  .&#039;var $toolTipVarName = new Tips($$(&amp;quot;&#039; . $toolTipVarName .&#039;&amp;quot;), {&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;className: &amp;quot;&#039; .$toolTipClassName .&#039;&amp;quot;,&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;initialize: function(){&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t\t&amp;quot;    .&#039;this.fx = new Fx.Style(this.toolTip, &amp;quot;opacity&amp;quot;, {duration: 500, wait: false}).set(0);&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;},&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;onShow: function(toolTip){&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t\t&amp;quot;    .&#039;this.fx.start(1);&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;},&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;onHide: function(toolTip) {&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t\t&amp;quot;    .&#039;this.fx.start(0);&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t\t&amp;quot;   .&#039;}&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &amp;quot;\t&amp;quot;  .&#039;});&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
    $javascript .= &#039;});&#039; .&amp;quot;\n\n&amp;quot;;&lt;br /&gt;
    return $javascript;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$document = JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addStyleSheet(&amp;quot;/joomla/components/com_mycustomcomponent/css/mytooltip.css&amp;quot;,&#039;text/css&#039;,&amp;quot;screen&amp;quot;);&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration(getToolTipJS(&amp;quot;mytool&amp;quot;,&amp;quot;MyToolTip&amp;quot;));&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Note that in order for this Javascript to be functionally useful, it would be necessary to include the appropriate class name in the HTML, as well as providing the &amp;lt;code&amp;gt;mytooltip.css&amp;lt;/code&amp;gt; file. Both are outside the scope of this article.&lt;br /&gt;
&lt;br /&gt;
== CSS Examples ==&lt;br /&gt;
&lt;br /&gt;
This is also useful if your inserting a parameter/form field of CSS into your code. For example in a module, you might want a user to choose to call the colour of the border. After calling the parameter/form field and assigning it a variable $bordercolor in mod_example.php. Then in tmpl/default.php you can include the following&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$document = JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addStyleSheet(JURI::base() . &#039;modules/mod_example/css/mod_example.css&#039;);&lt;br /&gt;
$style = &#039;#example {&lt;br /&gt;
	border-color:#&#039; . $bordercolor . &#039;;&lt;br /&gt;
	}&#039;;&lt;br /&gt;
$document-&amp;gt;addStyleDeclaration( $style );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here mod_example.css contains the CSS file of any non-parameter based styles. Then the bordercolor parameter/form field is added in separately&lt;br /&gt;
&lt;br /&gt;
==Add Custom Tag==&lt;br /&gt;
&lt;br /&gt;
There will be some occasions where even these functions are not flexible enough, as they are limited to writing the contents of &amp;lt;code&amp;gt;&amp;lt;script /&amp;gt;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;&amp;lt;style /&amp;gt;&amp;lt;/code&amp;gt; tags, and cannot add anything outside those tags. One example would be the inclusion of a stylesheet link within conditional comments, so that it is picked up only by Internet Explorer 6 and earlier. To do this, use &amp;lt;code&amp;gt;$document-&amp;gt;addCustomTag&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$stylelink = &#039;&amp;lt;!--[if lte IE 6]&amp;gt;&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
$stylelink .= &#039;&amp;lt;link rel=&amp;quot;stylesheet&amp;quot; href=&amp;quot;../css/IEonly.css&amp;quot; /&amp;gt;&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
$stylelink .= &#039;&amp;lt;![endif]--&amp;gt;&#039; .&amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$document = JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addCustomTag($stylelink);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]] [[Category:Tutorials]][[Category:Component Development]][[Category:Module Development]]&lt;br /&gt;
[[Category:JavaScript]]&lt;br /&gt;
[[Category:CSS]]&lt;br /&gt;
&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>Chivitli</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=102235</id>
		<title>Adding JavaScript</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=102235"/>
		<updated>2013-08-12T06:36:56Z</updated>

		<summary type="html">&lt;p&gt;Chivitli: /* External JavaScript */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{version/tutor|1.5,2.5,3.1}}&lt;br /&gt;
&lt;br /&gt;
JavaScript (also known as ECMAScript) is a scripting language primarily used in client-side web site development to extend and enhance an end-user&#039;s experience. Joomla provides developers with easy-to-use mechanisms to include JavaScript in their extensions using existing API methods.&lt;br /&gt;
&lt;br /&gt;
There are a number of methods for including JavaScript in your Joomla extensions; some of these are described below.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
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&#039;s View class (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/view.html.php) or template script (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php or in the case of a module, in its template script (&amp;lt;yourmodule&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php). &lt;br /&gt;
&lt;br /&gt;
===Inline JavaScript===&lt;br /&gt;
&lt;br /&gt;
Blocks of JavaScript code can be declared directly within a component or module&#039;s display template using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration addScriptDeclaration] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
    window.event(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
        alert(&amp;quot;An inline JavaScript Declaration&amp;quot;);&lt;br /&gt;
    });&lt;br /&gt;
&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===External JavaScript===&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript addScript] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScript(&#039;/media/system/js/sample.js&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second uses the [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html JHTML] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// Add the path parameter if the path is different than &#039;media/system/js/&#039;&lt;br /&gt;
JHTML::script(&#039;sample.js&#039;, &#039;templates/custom/js/&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
JHtml::script(Juri::base() . &#039;templates/custom/js/sample.js&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
The Joomla API&#039;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&#039;s index.php via the jdoc head tag:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;jdoc:include type=&amp;quot;head&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot; src=&amp;quot;/media/system/js/sample.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Calling the class method [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration] would render the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot;&amp;gt;&lt;br /&gt;
window.addEvent(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
    alert(&amp;quot;Embedded block of JS here&amp;quot;);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 &amp;lt;head&amp;gt;&amp;lt;/head&amp;gt; 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).&lt;br /&gt;
&lt;br /&gt;
== Using a JavaScript Framework ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Two [[Javascript Frameworks]] are provided as part of Joomla 3.x; jQuery and Mootools. jQuery is a newly introduced framework which integrates with Joomla&#039;s new Bootstrap HTML framework; Mootools is Joomla&#039;s legacy Javascript library which is now superseded by jQuery and is included for backwards compatibility with 3rd party extensions.&lt;br /&gt;
&lt;br /&gt;
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&#039;s API.&lt;br /&gt;
&lt;br /&gt;
===Joomla 3.x jQuery ===&lt;br /&gt;
Please see the guide on [[Javascript_Frameworks|Javascript Frameworks in Joomla 3.x]] for information about including a framework in Joomla 3.x&lt;br /&gt;
&lt;br /&gt;
=== Joomla 1.5/2.5 Mootools ===&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&lt;br /&gt;
FOR JOOMLA 1.5&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JHTML::_(&#039;behavior.mootools&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
FOR JOOMLA 2.5&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JHtml::_(&#039;behavior.framework&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Then using Mootools is almost identical to jQuery:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JFactory::getDocument()-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
window.addEvent(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
    alert($(&amp;quot;list&amp;quot;).getElements(&amp;quot;li&amp;quot;).length);&lt;br /&gt;
});&lt;br /&gt;
&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
More information about Mootools is available at http://mootools.net/. For API documentation, visit http://mootools.net/docs/core.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
&lt;br /&gt;
[[JHtmlBehavior::framework/11.1| JHtmlBehaviour::framework method]] from the Joomla! Framework 11.1 documentation&lt;br /&gt;
&lt;br /&gt;
[[Ajax using MooTools]]&lt;br /&gt;
&lt;br /&gt;
[[Adding Javascript moo.fx to your component]]&lt;br /&gt;
&lt;br /&gt;
== External Links ==&lt;br /&gt;
http://api.joomla.org/Joomla-Platform/HTML/JHtmlBehavior.html#methodframework&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/JavaScript&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
http://mootools.net/&lt;br /&gt;
&lt;br /&gt;
http://jquery.com/&lt;br /&gt;
&lt;br /&gt;
[[Category:Templates]]&lt;br /&gt;
[[Category:Modules]]&lt;br /&gt;
[[Category:Components]]&lt;br /&gt;
[[Category:Plugins]]&lt;br /&gt;
[[Category:Tutorials]]&lt;br /&gt;
[[Category:JavaScript]]&lt;/div&gt;</summary>
		<author><name>Chivitli</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=102234</id>
		<title>Adding JavaScript</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=102234"/>
		<updated>2013-08-12T06:27:49Z</updated>

		<summary type="html">&lt;p&gt;Chivitli: /* External JavaScript */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{version/tutor|1.5,2.5,3.1}}&lt;br /&gt;
&lt;br /&gt;
JavaScript (also known as ECMAScript) is a scripting language primarily used in client-side web site development to extend and enhance an end-user&#039;s experience. Joomla provides developers with easy-to-use mechanisms to include JavaScript in their extensions using existing API methods.&lt;br /&gt;
&lt;br /&gt;
There are a number of methods for including JavaScript in your Joomla extensions; some of these are described below.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
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&#039;s View class (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/view.html.php) or template script (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php or in the case of a module, in its template script (&amp;lt;yourmodule&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php). &lt;br /&gt;
&lt;br /&gt;
===Inline JavaScript===&lt;br /&gt;
&lt;br /&gt;
Blocks of JavaScript code can be declared directly within a component or module&#039;s display template using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration addScriptDeclaration] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
    window.event(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
        alert(&amp;quot;An inline JavaScript Declaration&amp;quot;);&lt;br /&gt;
    });&lt;br /&gt;
&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===External JavaScript===&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript addScript] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScript(&#039;/media/system/js/sample.js&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second uses the [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html JHTML] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// Add the path parameter if the path is different than &#039;media/system/js/&#039;&lt;br /&gt;
JHTML::script(&#039;sample.js&#039;, &#039;templates/custom/js/&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 absolute link to your javacript:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
JHtml::script(Juri::base() . &#039;templates/custom/js/sample.js&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
The Joomla API&#039;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&#039;s index.php via the jdoc head tag:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;jdoc:include type=&amp;quot;head&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot; src=&amp;quot;/media/system/js/sample.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Calling the class method [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration] would render the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot;&amp;gt;&lt;br /&gt;
window.addEvent(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
    alert(&amp;quot;Embedded block of JS here&amp;quot;);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 &amp;lt;head&amp;gt;&amp;lt;/head&amp;gt; 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).&lt;br /&gt;
&lt;br /&gt;
== Using a JavaScript Framework ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Two [[Javascript Frameworks]] are provided as part of Joomla 3.x; jQuery and Mootools. jQuery is a newly introduced framework which integrates with Joomla&#039;s new Bootstrap HTML framework; Mootools is Joomla&#039;s legacy Javascript library which is now superseded by jQuery and is included for backwards compatibility with 3rd party extensions.&lt;br /&gt;
&lt;br /&gt;
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&#039;s API.&lt;br /&gt;
&lt;br /&gt;
===Joomla 3.x jQuery ===&lt;br /&gt;
Please see the guide on [[Javascript_Frameworks|Javascript Frameworks in Joomla 3.x]] for information about including a framework in Joomla 3.x&lt;br /&gt;
&lt;br /&gt;
=== Joomla 1.5/2.5 Mootools ===&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&lt;br /&gt;
FOR JOOMLA 1.5&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JHTML::_(&#039;behavior.mootools&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
FOR JOOMLA 2.5&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JHtml::_(&#039;behavior.framework&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Then using Mootools is almost identical to jQuery:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JFactory::getDocument()-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
window.addEvent(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
    alert($(&amp;quot;list&amp;quot;).getElements(&amp;quot;li&amp;quot;).length);&lt;br /&gt;
});&lt;br /&gt;
&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
More information about Mootools is available at http://mootools.net/. For API documentation, visit http://mootools.net/docs/core.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
&lt;br /&gt;
[[JHtmlBehavior::framework/11.1| JHtmlBehaviour::framework method]] from the Joomla! Framework 11.1 documentation&lt;br /&gt;
&lt;br /&gt;
[[Ajax using MooTools]]&lt;br /&gt;
&lt;br /&gt;
[[Adding Javascript moo.fx to your component]]&lt;br /&gt;
&lt;br /&gt;
== External Links ==&lt;br /&gt;
http://api.joomla.org/Joomla-Platform/HTML/JHtmlBehavior.html#methodframework&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/JavaScript&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
http://mootools.net/&lt;br /&gt;
&lt;br /&gt;
http://jquery.com/&lt;br /&gt;
&lt;br /&gt;
[[Category:Templates]]&lt;br /&gt;
[[Category:Modules]]&lt;br /&gt;
[[Category:Components]]&lt;br /&gt;
[[Category:Plugins]]&lt;br /&gt;
[[Category:Tutorials]]&lt;br /&gt;
[[Category:JavaScript]]&lt;/div&gt;</summary>
		<author><name>Chivitli</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=102233</id>
		<title>Adding JavaScript</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=102233"/>
		<updated>2013-08-12T06:26:55Z</updated>

		<summary type="html">&lt;p&gt;Chivitli: /* External JavaScript */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{version/tutor|1.5,2.5,3.1}}&lt;br /&gt;
&lt;br /&gt;
JavaScript (also known as ECMAScript) is a scripting language primarily used in client-side web site development to extend and enhance an end-user&#039;s experience. Joomla provides developers with easy-to-use mechanisms to include JavaScript in their extensions using existing API methods.&lt;br /&gt;
&lt;br /&gt;
There are a number of methods for including JavaScript in your Joomla extensions; some of these are described below.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
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&#039;s View class (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/view.html.php) or template script (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php or in the case of a module, in its template script (&amp;lt;yourmodule&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php). &lt;br /&gt;
&lt;br /&gt;
===Inline JavaScript===&lt;br /&gt;
&lt;br /&gt;
Blocks of JavaScript code can be declared directly within a component or module&#039;s display template using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration addScriptDeclaration] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
    window.event(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
        alert(&amp;quot;An inline JavaScript Declaration&amp;quot;);&lt;br /&gt;
    });&lt;br /&gt;
&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===External JavaScript===&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript addScript] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScript(&#039;/media/system/js/sample.js&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second uses the [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html JHTML] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// Add the path parameter if the path is different than &#039;media/system/js/&#039;&lt;br /&gt;
JHTML::script(&#039;sample.js&#039;, &#039;templates/custom/js/&#039;);&lt;br /&gt;
&lt;br /&gt;
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 absolute link to your javacript:&lt;br /&gt;
&lt;br /&gt;
JHtml::script(Juri::base() . &#039;templates/custom/js/sample.js&#039;);&lt;br /&gt;
&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
The Joomla API&#039;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&#039;s index.php via the jdoc head tag:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;jdoc:include type=&amp;quot;head&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot; src=&amp;quot;/media/system/js/sample.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Calling the class method [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration] would render the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot;&amp;gt;&lt;br /&gt;
window.addEvent(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
    alert(&amp;quot;Embedded block of JS here&amp;quot;);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 &amp;lt;head&amp;gt;&amp;lt;/head&amp;gt; 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).&lt;br /&gt;
&lt;br /&gt;
== Using a JavaScript Framework ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Two [[Javascript Frameworks]] are provided as part of Joomla 3.x; jQuery and Mootools. jQuery is a newly introduced framework which integrates with Joomla&#039;s new Bootstrap HTML framework; Mootools is Joomla&#039;s legacy Javascript library which is now superseded by jQuery and is included for backwards compatibility with 3rd party extensions.&lt;br /&gt;
&lt;br /&gt;
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&#039;s API.&lt;br /&gt;
&lt;br /&gt;
===Joomla 3.x jQuery ===&lt;br /&gt;
Please see the guide on [[Javascript_Frameworks|Javascript Frameworks in Joomla 3.x]] for information about including a framework in Joomla 3.x&lt;br /&gt;
&lt;br /&gt;
=== Joomla 1.5/2.5 Mootools ===&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&lt;br /&gt;
FOR JOOMLA 1.5&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JHTML::_(&#039;behavior.mootools&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
FOR JOOMLA 2.5&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JHtml::_(&#039;behavior.framework&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Then using Mootools is almost identical to jQuery:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JFactory::getDocument()-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
window.addEvent(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
    alert($(&amp;quot;list&amp;quot;).getElements(&amp;quot;li&amp;quot;).length);&lt;br /&gt;
});&lt;br /&gt;
&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
More information about Mootools is available at http://mootools.net/. For API documentation, visit http://mootools.net/docs/core.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
&lt;br /&gt;
[[JHtmlBehavior::framework/11.1| JHtmlBehaviour::framework method]] from the Joomla! Framework 11.1 documentation&lt;br /&gt;
&lt;br /&gt;
[[Ajax using MooTools]]&lt;br /&gt;
&lt;br /&gt;
[[Adding Javascript moo.fx to your component]]&lt;br /&gt;
&lt;br /&gt;
== External Links ==&lt;br /&gt;
http://api.joomla.org/Joomla-Platform/HTML/JHtmlBehavior.html#methodframework&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/JavaScript&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
http://mootools.net/&lt;br /&gt;
&lt;br /&gt;
http://jquery.com/&lt;br /&gt;
&lt;br /&gt;
[[Category:Templates]]&lt;br /&gt;
[[Category:Modules]]&lt;br /&gt;
[[Category:Components]]&lt;br /&gt;
[[Category:Plugins]]&lt;br /&gt;
[[Category:Tutorials]]&lt;br /&gt;
[[Category:JavaScript]]&lt;/div&gt;</summary>
		<author><name>Chivitli</name></author>
	</entry>
</feed>