2

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).

2 Answers 2

1

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..

Sign up to request clarification or add additional context in comments.

Comments

1

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')

1 Comment

Nice..yes you can use this method too, if the script file have only limited lines you can use addScriptDeclaration check this too docs.joomla.org/Adding_JavaScript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.