Actions

Difference between revisions of "JDocumentHTML/addCustomTag"

From Joomla! Documentation

m (Added an example.)
(Example)
Line 21: Line 21:
 
$doc=>addCustomTag( '<!-- This is a comment. -->' );
 
$doc=>addCustomTag( '<!-- This is a comment. -->' );
 
</source>
 
</source>
 +
 +
Adding dynamic javascript to the HTML head section:
 +
<source lang="php">
 +
function getJavaScript($message){
 +
    $javascript = "<script type=\"text/javascript\">\n\n";
 +
    $javascript .= "if(window.addEventListener){ // Mozilla, Netscape, Firefox";
 +
    $javascript .= "    object.addEventListener(\"load\", function{ alert(\"$message\");}, false);";
 +
    $javascript .= "} else { // IE";
 +
    $javascript .= "    object.attachEvent(\"onload\", function{ alert(\"$message\");});";
 +
    $javascript .= "}";
 +
    $javascript .= "</script>\n\n";
 +
    return $javascript;
 +
}
 +
 +
$doc =& JFactory::getDocument();
 +
$doc=>addCustomTag( getJavaScript("This will appear in an alert box after the page loads.") );
 +
</source>
 +
 
===See also===
 
===See also===
 
* [http://api.joomla.org/Joomla-Framework/Document/JDocumentHTML.html#addCustomTag JDocumentHTML->addCustomTag on api.joomla.org]
 
* [http://api.joomla.org/Joomla-Framework/Document/JDocumentHTML.html#addCustomTag JDocumentHTML->addCustomTag on api.joomla.org]
 
<noinclude>[[Category:Development]][[Category:Framework]][[Category:JDocumentHTML]]</noinclude>
 
<noinclude>[[Category:Development]][[Category:Framework]][[Category:JDocumentHTML]]</noinclude>

Revision as of 04:37, 13 February 2009

Adds a custom HTML string to the document head.

Syntax

void addCustomTag( $html )

where:

Argument Data type Description Default
$html string Custom HTML string to be added.

Example

Adding a comment to the HTML head section:

$doc =& JFactory::getDocument();
$doc=>addCustomTag( '<!-- This is a comment. -->' );

Adding dynamic javascript to the HTML head section:

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

See also