Actions

JDocument/addScript

From Joomla! Documentation

Adds the definition of a linked external script to the document object. Duplicates are ignored.

Syntax

void addScript( $url, $type, $defer, $async )

where:

Argument Data type Description Default
$url string URL of script.
$type string MIME type of script. 'text/javascript'
$defer boolean Whether to add the HTML `defer` attribute false
$async boolean Whether to add the HTML `async` attribute false

Example

To add a link to some JavaScript code at a specific URL, you could use:

$doc = JFactory::getDocument();
$doc->addScript("http://www.example.com/js/myscript.js");

How this is rendered depends on the document type. If the document type is HTML then this code will produce following link in the HTML HEAD section:

<script type="text/javascript" src="http://www.example.com/js/myscript.js"></script>

Note that this does not have some of the features that JHtml::script has, such as searching for minified files.

See also