0

I am developing a custom component for Joomla 3.x. I added an external JavaScript file to one of my components front-end views called "pages" located in \com_mycomponent\views\pages\view.html.php within the function display(). I am using this code in this function:

$document = JFactory::getDocument();
$document->addScript(JURI::root().'media/com_mycomponent/js/gallery.js');

When I check with firebug I see the file has been loaded but the problem is with the ordering. As you see below it comes before jquery.min.js. I also tried to add this file in \com_mycomponent\views\pages\tmpl\default.php using the same code, but the same problem happened. This is what I get from firebug.

<script type="text/javascript" src="http://localhost/tester3/media/com_mycomponent/js/gallery.js">
<script type="text/javascript" src="/tester3/media/jui/js/jquery.min.js">
<script type="text/javascript" src="/tester3/media/jui/js/jquery-noconflict.js">
<script type="text/javascript" src="/tester3/media/jui/js/jquery-migrate.min.js">

I appreciate your help.

1 Answer 1

1

I'm not 100% sure if the JHtml method will deal with the ordering issue but best give it a try. So replace this:

$document = JFactory::getDocument();
$document->addScript(JURI::root().'media/com_mycomponent/js/gallery.js');

with this:

JHtml::_('bootstrap.framework');
JHtml::_('jquery.framework');
JHtml::_('script', JUri::root() . 'media/com_mycomponent/js/gallery.js');
Sign up to request clarification or add additional context in comments.

6 Comments

I tried your recommendation both in view.html.php and default.php but the problem still exists which is very strange!!!!
@Gaz - Try using JHtml::_('jquery.framework'); before as shown above
It kinda worked this time, but it still comes before bootstrap.min.js and template.js
It shouldn't be a problem if it comes before template.js however to ensure it comes after bootstrap.min.js, see my updated answer.
I mean this the normal way of adding external js files to componets?
|

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.