0

I'm building a component view in Joomla and I'm using a lot of JS in the file inside the component myview/tmpl/default.php.

Where is the best folder to store a separate JS file in a component and how to get a link to that file inside the default.php

amitgur
  • 407
  • 6
  • 11

3 Answers3

1

The recommended location for any web accessible media (images, javascript, css etc) required by a Joomla! Extension (component, module etc) is the /media directory. This is covered in the Manifest documentation on the Joomla! docs website.

To put it simply, in your extensions directory include a directory called media (or something meaningful to you). In that store all of your web accessible content, you can even have sub-directories like images or js or css or all of them.

Then in your manifest XML add the media element, like:

    <media folder="media" destination="com_example">
            <filename>logo.png</filename>
            <folder>css</folder>
            <folder>js</folder>
    </media>

The Joomla installer will then create a folder for you inside /media called com_example, then you can access all your media files with calls like:

$document->addStyleSheet(JURI::root() . 'media/com_example/css/example.css');
$document->addScript(JURI::root() . 'media/com_example/css/example.js');

or

<img src="<?php echo JURI::root() . 'media/com_example/'; ?>logo.png"
Craig
  • 9,335
  • 2
  • 34
  • 38
  • Thanks for your answer @cppl but I ran into a new problem. generating the site in a multi-language environment I'm using JText inside the JS so I can't use a .js file. I'm stacked again with too long files with JS and PHP. any solution for that ? – amitgur May 01 '13 at 20:34
  • Yes - see this answer on using JText in Javascript. http://stackoverflow.com/questions/16122021/add-language-constants-to-joomla-component-javascript/16136981#16136981 – Craig May 02 '13 at 03:34
0

You can try this

<?php echo JURI::root();?>components/com_mycomponent
Praveen kalal
  • 2,148
  • 4
  • 19
  • 33
  • This is surly a good solution but I think cppl described the correct answer for the problem.. thanks @Praveen kalal – amitgur May 01 '13 at 20:36
0

This will give you path to components/CURRENT COMPONENT.

echo JPATH_COMPONENT;

Source

Fury
  • 4,643
  • 5
  • 50
  • 80