JDocumentHTML/addCustomTag
From Joomla! Documentation
< JDocumentHTML(Difference between revisions)
(Added example of adding a script to the head section of a webpage using the addCustomTag API.) |
m (→Example) |
||
| Line 29: | Line 29: | ||
$javascript .= " object.addEventListener(\"load\", function(){ alert(\"$message\");}, false);"; | $javascript .= " object.addEventListener(\"load\", function(){ alert(\"$message\");}, false);"; | ||
$javascript .= "} else { // IE"; | $javascript .= "} else { // IE"; | ||
| − | $javascript .= " object.attachEvent(\"onload\", function{ alert(\"$message\");});"; | + | $javascript .= " object.attachEvent(\"onload\", function(){ alert(\"$message\");});"; |
$javascript .= "}"; | $javascript .= "}"; | ||
$javascript .= "</script>\n\n"; | $javascript .= "</script>\n\n"; | ||
Revision as of 05:04, 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.") );
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.