Actions

Difference between revisions of "JDocumentHTML/addCustomTag"

From Joomla! Documentation

m (Added an example.)
m (Minor formatting. Fixed code example.)
 
(4 intermediate revisions by one user not shown)
Line 15: Line 15:
 
|
 
|
 
|}
 
|}
===Example===
+
===Example 1===
 
Adding a comment to the HTML head section:
 
Adding a comment to the HTML head section:
 
<source lang="php">
 
<source lang="php">
 
$doc =& JFactory::getDocument();
 
$doc =& JFactory::getDocument();
$doc=>addCustomTag( '<!-- This is a comment. -->' );
+
$doc->addCustomTag( '<!-- This is a comment. -->' );
 
</source>
 
</source>
 +
 +
===Example 2===
 +
Adding dynamic JavaScript to the HTML head section (an alternative to using [[JDocument/addScriptDeclaration|JDocument->addScriptDeclaration]]):
 +
<source lang="php">
 +
function getJavaScript($message) {
 +
    $javascript = "<script type=\"text/javascript\">\n";
 +
    $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 .= "}\n";
 +
    $javascript .= "</script>\n";
 +
    return $javascript;
 +
}
 +
 +
$doc =& JFactory::getDocument();
 +
$doc->addCustomTag( getJavaScript( 'This will appear in an alert box after the page loads.' ) );
 +
</source>
 +
 +
It would have been just as easy to get the string from a database and pass it to <code>getJavaScript</code> instead of using the constant string in the example.
 +
 
===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>

Latest revision as of 10:53, 15 February 2009

Adds a custom HTML string to the document head.

Contents

Syntax

void addCustomTag( $html )

where:

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

Example 1

Adding a comment to the HTML head section:

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

Example 2

Adding dynamic JavaScript to the HTML head section (an alternative to using JDocument->addScriptDeclaration):

function getJavaScript($message) {
    $javascript = "<script type=\"text/javascript\">\n";
    $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 .= "}\n";
    $javascript .= "</script>\n";
    return $javascript;
}
 
$doc =& JFactory::getDocument();
$doc->addCustomTag( getJavaScript( 'This will appear in an alert box after the page loads.' ) );

It would have been just as easy to get the string from a database and pass it to getJavaScript instead of using the constant string in the example.

See also