Alon
Alon

Reputation: 3906

How to insert an external Javascript file to my joomla 1.5 <head> tag of each page

I'm trying to build a module for joomla 1.5 which basically need to load my javascript file to the part of each webpage. I've googled all possibilities and tried every way I read in the joomla docs and nothing succeeded. It's very easy to add javascript to the body but I want to add it to the part of the code.

Thanks

Upvotes: 1

Views: 2258

Answers (2)

Kristian Hildebrandt
Kristian Hildebrandt

Reputation: 1528

You should not insert the javascript directly into the head tag, since it will not be able to be compressed and combined with other scripts. And also, since it is hard coded into the head tag, you will have to manually add/edit/remove it for every possible template.

Just put this into the module code:

<?php 
      $document = &JFactory::getDocument();
      $document->addScript( '/path/to/my.js' );
?>

If you want your javascript to be added to every page, you will have to publish your module on every page. It is probably a better idea to write a plugin using the above code sample.

Upvotes: 3

Hanny
Hanny

Reputation: 2159

You should be able to put something into your index.php file (although not recommended because IIRC it will be overwritten when you update your Joomla (which you should be doing)):

<script type="text/javascript" src="<?php echo $this->baseurl ?>/media/system/js/myjavascript.js"></script>

That should get your script added to every page where you want it.

*Edit: that was only for getting JS to load on every page - for modules you'll have to use a plugin of some sort. Something like THIS may help I think...

Although, I'm still moderately unclear as to where/how you're looking to implement your JS.

Upvotes: 0

Related Questions