Nick
Nick

Reputation: 84

How can include component js after templete js

I added javascript on templete index.php

$doc    =   JFactory::getDocument();
$doc->addScript($this->baseurl . '/templates/' . $this->template . '/js/jquery.js', 'text/javascript');

and added another on component below

$document = JFactory::getDocument();      
$document->addScript($this->baseurl . '/templates/' . $this->template . '/js/validation.js');  

but always js (validation.js) of component getting add before tempelete js(jquery.js)

How can i add component js(validation.js) after templete js(jquery.js).

Upvotes: 2

Views: 658

Answers (2)

Jobin
Jobin

Reputation: 8282

If you are using jquery.js as main jQuery library on your project then you can simple include the jquery.js before the head module in index.php. like below.

<script src="templates/js/jquery.js" type="text/javascript"></script>
<jdoc:include type="head" />

Then in your component you can simply use same as below.

$document = JFactory::getDocument();      
$document->addScript($this->baseurl . '/templates/' . $this->template . '/js/validation.js');

An alternate option is Addcustom tag , helps to adding scripts to the document.

$document   =   JFactory::getDocument();
$document->addCustomTag('<script src="'.$this->baseurl . '/templates/' . $this->template . '/js/validation.js" type="text/javascript"></script>');

Otherwise you have to load the component js also in index.php with second order but that will load the js file for all pages.

Hope its helps..

Upvotes: 1

Nick
Nick

Reputation: 84

I am going through google and found one solution that i can use custom tag function for component like below:

$document   =   JFactory::getDocument();
$document->addCustomTag('<script src="'.$this->baseurl . '/templates/' . $this->template . '/js/validation.js" type="text/javascript"></script>');

and for template as it below:

$doc    =   JFactory::getDocument();
$doc->addScript($this->baseurl . '/templates/' . $this->template . '/js/jquery.js', 'text/javascript')

Upvotes: 1

Related Questions