Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

JavaScript is a dynamically-typed language commonly used for client-side scripting. Unless a tag for a framework/library is also included, a pure JavaScript answer is expected.


JavaScript is a dynamic, object-oriented, prototype-based, weakly typed language commonly used for client-side scripting in web browsers. Despite the name, it is unrelated to the Java programming language and shares only superficial similarities.

Unless a tag for a framework/library is also included, a pure JavaScript answer is expected for questions with the javascript tag.


JavaScript runs on nearly every OS, and an engine is included in almost every mainstream web browser. Developed in 1995 by Brendan Eich at Netscape Communications, it was originally called LiveScript but was renamed to JavaScript due to Netscape's friendly relationship with Sun Microsystems at the time.

Adding javascript to Joomla

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
Blocks of JavaScript code can be declared directly within a component or module's display template using the JDocument class' addScriptDeclaration method:

$document = JFactory::getDocument();
    window.event("domready", function() {
        alert("An inline JavaScript Declaration");

External JavaScript
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:

$document = JFactory::getDocument();

The second uses the JHTML class' script method:

// 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:

JHtml::script(Juri::base() . 'templates/custom/js/sample.js');
history | excerpt history