JDocumentHTML/addCustomTag
From Joomla! Documentation
< JDocumentHTML(Difference between revisions)
(New page: Adds a custom HTML string to the document head. ===Syntax=== void addCustomTag( $html ) where: {| class="wikitable" !Argument !Data type !Description !Default |- |$html |string |Custom HT...) |
m (Minor formatting. Fixed code example.) |
||
| (5 intermediate revisions by 2 users not shown) | |||
| Line 15: | Line 15: | ||
| | | | ||
|} | |} | ||
| − | ===Example=== | + | ===Example 1=== |
| + | Adding a comment to the HTML head section: | ||
| + | <source lang="php"> | ||
| + | $doc =& JFactory::getDocument(); | ||
| + | $doc->addCustomTag( '<!-- This is a comment. -->' ); | ||
| + | </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 |
[edit] Syntax
void addCustomTag( $html )
where:
| Argument | Data type | Description | Default |
|---|---|---|---|
| $html | string | Custom HTML string to be added. |
[edit] Example 1
Adding a comment to the HTML head section:
$doc =& JFactory::getDocument(); $doc->addCustomTag( '<!-- This is a comment. -->' );
[edit] 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.