Actions

Difference between revisions of "JDocument/addScript"

From Joomla! Documentation

m (Example depends on document type.)
(See also: Add link to JHTML::script)
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
Adds the definition of a linked external script to the document object.  Duplicates are ignored.
 
Adds the definition of a linked external script to the document object.  Duplicates are ignored.
 
===Syntax===
 
===Syntax===
void addScript( $url, $type )
+
void addScript( $url, $type, $defer, $async )
  
 
where:
 
where:
Line 19: Line 19:
 
|MIME type of script.
 
|MIME type of script.
 
|'text/javascript'
 
|'text/javascript'
 +
|-
 +
|$defer
 +
|boolean
 +
|Whether to add the HTML `defer` attribute
 +
|false
 +
|-
 +
|$async
 +
|boolean
 +
|Whether to add the HTML `async` attribute
 +
|false
 
|}
 
|}
 
===Example===
 
===Example===
 
To add a link to some JavaScript code at a specific URL, you could use:
 
To add a link to some JavaScript code at a specific URL, you could use:
 
<source lang="php">
 
<source lang="php">
$doc =& JFactory::getDocument();
+
$doc = JFactory::getDocument();
 
$doc->addScript("http://www.example.com/js/myscript.js");
 
$doc->addScript("http://www.example.com/js/myscript.js");
 
</source>
 
</source>
Line 30: Line 40:
 
<script type="text/javascript" src="http://www.example.com/js/myscript.js"></script>
 
<script type="text/javascript" src="http://www.example.com/js/myscript.js"></script>
 
</source>
 
</source>
 +
 +
Note that this does not have some of the features that JHtml::script has, such as searching for minified files.
  
 
===See also===
 
===See also===
* [http://api.joomla.org/Joomla-Framework/Document/JDocument.html#addScript JDocument->addScript on api.joomla.org]
+
* [http://api.joomla.org/cms-3/classes/JDocument.html#method_addScript JDocument->addScript on api.joomla.org]
 
* [[JDocument/addScriptDeclaration|JDocument->addScriptDeclaration]]
 
* [[JDocument/addScriptDeclaration|JDocument->addScriptDeclaration]]
 +
* [https://api.joomla.org/cms-3/classes/JHtml.html#method_script JHtml::script on api.joomla.org]
 
<noinclude>[[Category:Development]][[Category:Framework]][[Category:JDocument]]</noinclude>
 
<noinclude>[[Category:Development]][[Category:Framework]][[Category:JDocument]]</noinclude>

Latest revision as of 06:38, 22 May 2015

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