Adds a custom HTML string to the document head.


void addCustomTag( $html )


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