Actions

Difference between revisions of "JDocument/addScriptDeclaration"

From Joomla! Documentation

m (Added second example, similar to JDocument->addCustomTag.)
(Marked this version for translation)
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
Adds an internal script to the document object.  The script is appended to the document objects' internal script buffer for the specified type.
+
<noinclude><languages /></noinclude>
===Syntax===
+
<translate><!--T:1-->
void addScriptDeclaration( $content, $type )
+
Adds an internal script to the document object.  The script is appended to the document objects' internal script buffer for the specified type.</translate>
 +
<translate>===Syntax=== <!--T:2-->
 +
void addScriptDeclaration( $content, $type )</translate>
  
where:
+
<translate><!--T:3-->
 +
where:</translate>
 
{| class="wikitable"
 
{| class="wikitable"
!Argument
+
!<translate><!--T:4-->
!Data type
+
Argument</translate>
!Description
+
!<translate><!--T:5-->
!Default
+
Data type</translate>
 +
!<translate><!--T:6-->
 +
Description</translate>
 +
!<translate><!--T:7-->
 +
Default</translate>
 
|-
 
|-
 
|$content
 
|$content
 
|string
 
|string
|Script content.
+
|<translate><!--T:8-->
 +
Script content.</translate>
 
|
 
|
 
|-
 
|-
 
|$type
 
|$type
 
|string
 
|string
|MIME type of script.
+
|<translate><!--T:9-->
 +
MIME type of script.</translate>
 
|'text/javascript'
 
|'text/javascript'
 
|}
 
|}
===Example 1===
+
<translate>===Example 1=== <!--T:10-->
To add a Hello World alert in JavaScript, you could use:
+
To add a Hello World alert in JavaScript, you could use:</translate>
 +
 
 
<source lang="php">
 
<source lang="php">
 
$content = 'alert( \'Hello Joomla!\' )';
 
$content = 'alert( \'Hello Joomla!\' )';
Line 27: Line 37:
 
$doc->addScriptDeclaration( $content );
 
$doc->addScriptDeclaration( $content );
 
</source>
 
</source>
Notice that quotation marks in the JavaScript code will need to be escaped by preceding them with a backslash character.
+
 
===Example 2===
+
<translate><!--T:11-->
Adding a slightly more realistic piece of JavaScript:
+
Notice that quotation marks in the JavaScript code will need to be escaped by preceding them with a backslash character.</translate>
 +
<translate>===Example 2=== <!--T:12-->
 +
Adding a slightly more realistic piece of JavaScript:</translate>
 +
 
 
<source lang="php">
 
<source lang="php">
 
function getJavaScript($message) {
 
function getJavaScript($message) {
Line 43: Line 56:
 
$doc->addScriptDeclaration( getJavaScript( 'This will appear in an alert box after the page loads.' ) );
 
$doc->addScriptDeclaration( getJavaScript( 'This will appear in an alert box after the page loads.' ) );
 
</source>
 
</source>
===See also===
+
 
* [http://api.joomla.org/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument->addScriptDeclaration on api.joomla.org]
+
<translate>===See also=== <!--T:13--></translate>
 +
* [https://api.joomla.org/cms-3/classes/JDocument.html#method_addScriptDeclaration JDocument->addScriptDeclaration on api.joomla.org]
 
* [[JDocument/addScript|JDocument->addScript]]
 
* [[JDocument/addScript|JDocument->addScript]]
<noinclude>[[Category:Development]][[Category:Framework]][[Category:JDocument]]</noinclude>
+
 
 +
<noinclude>
 +
<translate>
 +
<!--T:14-->
 +
[[Category:Development]]
 +
[[Category:Framework]]
 +
[[Category:JDocument]]
 +
</translate>
 +
</noinclude>

Latest revision as of 08:26, 23 June 2015

Other languages:
English • ‎français

Adds an internal script to the document object. The script is appended to the document objects' internal script buffer for the specified type.

Syntax

void addScriptDeclaration( $content, $type )

where:

Argument Data type Description Default
$content string Script content.
$type string MIME type of script. 'text/javascript'

Example 1

To add a Hello World alert in JavaScript, you could use:

$content = 'alert( \'Hello Joomla!\' )';
$doc =& JFactory::getDocument();
$doc->addScriptDeclaration( $content );

Notice that quotation marks in the JavaScript code will need to be escaped by preceding them with a backslash character.

Example 2

Adding a slightly more realistic piece of JavaScript:

function getJavaScript($message) {
    $javascript .= 'if(window.addEventListener){ // Mozilla, Netscape, Firefox' . "\n";
    $javascript .= '    window.addEventListener("load", function(){ alert("' . $message . '");}, false);' . "\n";
    $javascript .= '} else { // IE' . "\n";
    $javascript .= '    window.attachEvent("onload", function(){ alert("' . $message . '");});' . "\n";
    $javascript .= '}';
    return $javascript;
}
 
$doc =& JFactory::getDocument();
$doc->addScriptDeclaration( getJavaScript( 'This will appear in an alert box after the page loads.' ) );

See also