How to link css and JavaScript in Joomla template


You can add css and JavaScript file in Joomla template using getDocument method of JFactory class. Open index.php of template file and add following php code as par your requirement , below code shows us that how can add style or include a JavaScript file to Joomla template.

To add a CSS style to the template:

$doc =& JFactory::getDocument();
$doc->addStyleDeclaration('
#divid {background:none;}
.color {color:#919194;font-size:13px;}
');

To add a CSS stylesheet file to template :

$doc =& JFactory::getDocument();
$doc->addStyleSheet( 'templates/' . $this->template . '/css/newstylesheet.css' );
output of above code in browser
<link rel="stylesheet" href="yoursite/templates/css/newstylesheet.css" type="text/css" />

To add a javascript to the template:

$doc =& JFactory::getDocument();
$doc->addScriptDeclaration( 'alert(\'test\')' );

To add a javascript file to the template:

$doc =& JFactory::getDocument();
$doc->addScript('templates/' . $this->template . '/javascript/newjavascript.js');
output of above code in browser
<script type="text/javascript" src="/javascript/newjavascript.js"></script>

Please visit following link if you want to know more about Joomla JDocument

http://docs.joomla.org/JDocument

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s