Actions

Difference between revisions of "JDocumentHTML/addCustomTag"

From Joomla! Documentation

(Example)
m (Minor formatting. Fixed code example.)
 
(2 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>
  
Adding dynamic javascript to the HTML head section:
+
===Example 2===
 +
Adding dynamic JavaScript to the HTML head section (an alternative to using [[JDocument/addScriptDeclaration|JDocument->addScriptDeclaration]]):
 
<source lang="php">
 
<source lang="php">
function getJavaScript($message){
+
function getJavaScript($message) {
     $javascript = "<script type=\"text/javascript\">\n\n";
+
     $javascript = "<script type=\"text/javascript\">\n";
     $javascript .= "if(window.addEventListener){ // Mozilla, Netscape, Firefox";
+
     $javascript .= "if(window.addEventListener){ // Mozilla, Netscape, Firefox\n";
     $javascript .= "    object.addEventListener(\"load\", function{ alert(\"$message\");}, false);";
+
     $javascript .= "    window.addEventListener(\"load\", function(){ alert(\"$message\");}, false);\n";
     $javascript .= "} else { // IE";
+
     $javascript .= "} else { // IE\n";
     $javascript .= "    object.attachEvent(\"onload\", function{ alert(\"$message\");});";
+
     $javascript .= "    window.attachEvent(\"onload\", function(){ alert(\"$message\");});\n";
     $javascript .= "}";
+
     $javascript .= "}\n";
     $javascript .= "</script>\n\n";
+
     $javascript .= "</script>\n";
 
     return $javascript;
 
     return $javascript;
 
}
 
}
  
 
$doc =& JFactory::getDocument();
 
$doc =& JFactory::getDocument();
$doc=>addCustomTag( getJavaScript("This will appear in an alert box after the page loads.") );
+
$doc->addCustomTag( getJavaScript( 'This will appear in an alert box after the page loads.' ) );
 
</source>
 
</source>
  
In the above example it would have been just as easy to say, pull the string from a database and pass it to getJavaScript instead of using the const string that is in the example.
+
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