Reputation: 12138
I have a front-end component view that I need to include a .js file in the header of (header is being generated by Joomla, not hard-coded in default.php). The front-end component file is mysite.com/components/com_arrcard/views/tmpl/default.php; the javascript file is located at mysite.com/administrator/components/com_arrcard/js/CalendarPopup.js.
Here's what I tried in my default.php:
$document =& JFactory::getDocument();
$popUrl = JURI::root(true).'/website/documents/ccv.html';
$popImage = JURI::root(true).'/website/documents/cv_card.jpg';
$popPhone = JURI::root(true).'/website/documents/phone.html';
$document->addScript( JURI::root(true).'/administrator/components/com_arrcard/js/CalendarPopup.js' );
The $popImage and $popPhone variables are getting set correctly, but the script is not being added - when I look at the rendered page source, CalendarPopup.js isn't anywhere.
Can anyone help me out? What am I doing wrong here? I've also tried
$document->addScript('administrator/components/com_arrcard/js/CalendarPopup.js' );
That doesn't work either. The addScript code does work when including the exact same file in a view in the administrator com_arrcard component. Is there some limitation on where addScript works?
Upvotes: 3
Views: 4276
Reputation: 10563
Your code does look correct. Perhaps try another method below, the first line is to include JS while the other for CSS (just in case someone stumbles upon this in future looking for css include).
JHTML::script( 'javascript.js', 'components' . DS . 'com_component' . DS . 'lib' . DS . 'js' . DS );
JHTML::stylesheet( 'style.css', 'components' . DS . 'com_component' . DS . 'lib' . DS . 'css' . DS );
Ensure that you use the DS (directory separator) so that Joomla can fill in the correct / or \ depending on the OS the site is running on.
So in your case you would need this line of code:
JHTML::script( 'CalendarPopup.js', 'administrator' . DS . 'components'. DS .'com_arrcard'. DS .'js');
Upvotes: 2
Reputation: 11
The code you post should work. You can manipulate the JDocument at any point before the page is actually rendered and sent back, so that shouldn't be a problem either regardless of whether you do it in the template file or in the controller or view files (which is really where it should go from a better MVC standpoint).
Check to make sure in your template index.php file that the following tag is included:
It's possible that's in your admin template (the default one, likely), but not a custom template.. though it should.
An additional, easy, way to test is change your template to rhuk_milkyway temporarily to see if the code is injected properly there.
Upvotes: 0