However, your Javascript or CSS might not be located in a separate file&mdash;you might want to generate them using PHP. In this case you can write the script or stylesheet directly into the head of your document:

However, your Javascript or CSS might not be located in a separate file&mdash;you might want to generate them using PHP. In this case you can write the script or stylesheet directly into the head of your document:

<source lang="php">

<source lang="php">

+

$document =& JFactory::getDocument();

// Add Javascript

// Add Javascript

$document->addScriptDeclaration($javascript, $type);

$document->addScriptDeclaration($javascript, $type);

Line 83:

Line 83:

</source>

</source>

Here mod_example.css contains the CSS file of any non-parameter based styles. Then the bordercolor parameter/form field is added in separately

Here mod_example.css contains the CSS file of any non-parameter based styles. Then the bordercolor parameter/form field is added in separately

+

+

==Add Custom Tag==

There will be some occasions where even these functions are not flexible enough, as they are limited to writing the contents of <code><script /></code> or <code><style /></code> tags, and cannot add anything outside those tags. One example would be the inclusion of a stylesheet link within conditional comments, so that it is picked up only by Internet Explorer 6 and earlier. To do this, use <code>$document->addCustomTag</code>:

There will be some occasions where even these functions are not flexible enough, as they are limited to writing the contents of <code><script /></code> or <code><style /></code> tags, and cannot add anything outside those tags. One example would be the inclusion of a stylesheet link within conditional comments, so that it is picked up only by Internet Explorer 6 and earlier. To do this, use <code>$document->addCustomTag</code>:

Revision as of 06:50, 1 February 2013

Contents

Inserting from a File

To have a well-formed XHTML document, you must put all references to Javascript and CSS files within the <head> portion. Since Joomla! generates all the HTML that makes up a page before output, it is possible to add these references within the <head> tags from your extension. The simplest way to do this is to make use of the functionality built in to Joomla!:

// Add a reference to a Javascript file// The default path is 'media/system/js/'
JHTML::script($filename,$path,$mootools);// Add a reference to a CSS file// The default path is 'media/system/css/'
JHTML::stylesheet($filename,$path);

Using these functions, Joomla! will take care of any additional requirements. For example, if your Javascript requires Mootools, setting $mootools = true will automatically ensure that Mootools is loaded, if it has not already been done.

However, the above functions will not be flexible enough for every scenario, and so it is possible to tap into the underlying functionality instead. Of course, you will also need to manually code some of the steps that would be done automatically using the functions above.

First, get a reference to the current document object:

$document=& JFactory::getDocument();

For a stylesheet, use this code:

$document->addStyleSheet($url);

To add a Javascript file, use this code:

$document->addScript($url);

Note this will **NOT** include Mootools or jQuery. If your script requires Mootools or jQuery see Javascript_Frameworks for full details on how to include them (note jQuery can only be included natively on Joomla 3.0+).

Inserting from within a PHP file

However, your Javascript or CSS might not be located in a separate file—you might want to generate them using PHP. In this case you can write the script or stylesheet directly into the head of your document:

Note that in order for this Javascript to be functionally useful, it would be necessary to include the appropriate class name in the HTML, as well as providing the mytooltip.css file. Both are outside the scope of this article.

CSS Examples

This is also useful if your inserting a parameter/form field of CSS into your code. For example in a module, you might want a user to choose to call the colour of the border. After calling the parameter/form field and assigning it a variable $bordercolor in mod_example.php. Then in tmpl/default.php you can include the following

Here mod_example.css contains the CSS file of any non-parameter based styles. Then the bordercolor parameter/form field is added in separately

Add Custom Tag

There will be some occasions where even these functions are not flexible enough, as they are limited to writing the contents of <script /> or <style /> tags, and cannot add anything outside those tags. One example would be the inclusion of a stylesheet link within conditional comments, so that it is picked up only by Internet Explorer 6 and earlier. To do this, use $document->addCustomTag: