Wednesday, October 13, 2010

Adding script Decleration in joomla

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 )



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 )
 
 

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.' ) );
 

source:
http://docs.joomla.org/JDocument/addScriptDeclaration

No comments:

Post a Comment