Actions

Difference between revisions of "JDocument/addScript"

From Joomla! Documentation

(New page: Adds the definition of a linked external script to the document object. Duplicates are ignored. ===Syntax=== void addScript( $url, $type ) where: {| !Argument !Data type !Description !De...)
 
m (Example depends on document type.)
(3 intermediate revisions by 2 users not shown)
Line 4: Line 4:
  
 
where:
 
where:
{|
+
{| class="wikitable"
 
!Argument
 
!Argument
 
!Data type
 
!Data type
Line 23: Line 23:
 
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>
 +
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:
 +
<source lang="html4strict">
 +
<script type="text/javascript" src="http://www.example.com/js/myscript.js"></script>
 +
</source>
 +
 
===See also===
 
===See also===
 
* [http://api.joomla.org/Joomla-Framework/Document/JDocument.html#addScript JDocument->addScript on api.joomla.org]
 
* [http://api.joomla.org/Joomla-Framework/Document/JDocument.html#addScript JDocument->addScript on api.joomla.org]
 
* [[JDocument/addScriptDeclaration|JDocument->addScriptDeclaration]]
 
* [[JDocument/addScriptDeclaration|JDocument->addScriptDeclaration]]
 
<noinclude>[[Category:Development]][[Category:Framework]][[Category:JDocument]]</noinclude>
 
<noinclude>[[Category:Development]][[Category:Framework]][[Category:JDocument]]</noinclude>

Revision as of 11:15, 10 November 2008

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

Syntax

void addScript( $url, $type )

where:

Argument Data type Description Default
$url string URL of script.
$type string MIME type of script. 'text/javascript'

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>

See also