Actions

Difference between revisions of "JDocument/addScriptDeclaration"

From Joomla! Documentation

(New page: 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( $con...)
 
(See also: Framework => Platform)
 
(3 intermediate revisions by one user not shown)
Line 4: Line 4:
  
 
where:
 
where:
{|
+
{| class="wikitable"
 
!Argument
 
!Argument
 
!Data type
 
!Data type
Line 20: Line 20:
 
|'text/javascript'
 
|'text/javascript'
 
|}
 
|}
===Example===
+
===Example 1===
 
To add a Hello World alert in JavaScript, you could use:
 
To add a Hello World alert in JavaScript, you could use:
 
<source lang="php">
 
<source lang="php">
 
$content = 'alert( \'Hello Joomla!\' )';
 
$content = 'alert( \'Hello Joomla!\' )';
$doc = JFactory::getDocument();
+
$doc =& JFactory::getDocument();
 
$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.
 
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:
 +
<source lang="php">
 +
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.' ) );
 +
</source>
 
===See also===
 
===See also===
* [http://api.joomla.org/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument->addScriptDeclaration on api.joomla.org]
+
* [http://api.joomla.org/Joomla-Platform/Document/JDocument.html#addScriptDeclaration JDocument->addScriptDeclaration on api.joomla.org]
 
* [[JDocument/addScript|JDocument->addScript]]
 
* [[JDocument/addScript|JDocument->addScript]]
 
<noinclude>[[Category:Development]][[Category:Framework]][[Category:JDocument]]</noinclude>
 
<noinclude>[[Category:Development]][[Category:Framework]][[Category:JDocument]]</noinclude>

Latest revision as of 03:17, 25 September 2013

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

Contents

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