simekadam
simekadam

Reputation: 7384

How to add javascript to joomla module?

Hi I have problem with adding javascript into a joomla module..I've found some solution, but it's not working..

$document = &JFactory::getDocument();
$document->addScript("/career.js");    

These two lines I have in my module, but the script isn't in rendered site.. The file is in root of my web (for test purposes only).

Thanks for your help

Upvotes: 3

Views: 20982

Answers (8)

Nirav Zaveri
Nirav Zaveri

Reputation: 717

For all those who are landing on this page at a much later date, and looking for solution in Joomla 3.x

You can include a module of type "Custom HTML Advanced" which will NOT filter out any script or style tags. You can use it on any of the pages you want.

Hope this helps you :)

Upvotes: 0

Irfan
Irfan

Reputation: 7059

Make sure that you have this

<jdoc:include type="head" />

after head tag in template index.php

Upvotes: 0

Yesceeohhh
Yesceeohhh

Reputation: 116

Try this, it's working perfectly.

$document = &JFactory::getDocument();
$document->addScript('modules'.DS.'MOD_YOURMODULENAME'.DS.'javascript.js');

Upvotes: 7

simekadam
simekadam

Reputation: 7384

Ouch..The main problem is, that I am loading the page containing custom module with javascript is loaded via ajax..And all the plugins seems to be adding the js code into head and I only replace some elements into body of my page. exist some solution to this problem?I know I can have all the js logic into one file, but I am not sure it is good way. Is somehow possible to include js directly to module?So that the script tag will be within my document body and correctly included (i am not sure the code will be fired??)

Thanks

Upvotes: 0

eyeonu
eyeonu

Reputation: 211

You could also use the joomla php module http://www.fijiwebdesign.com/products/joomla-php-module.html or maybe you found a module that better fits your needs at: http://extensions.joomla.org/extensions/edition/custom-code-in-modules

Upvotes: 0

simekadam
simekadam

Reputation: 7384

So only workaround which works for me is to add this js to template of my site..But its really awful solution, since I only need it in one article/module

Upvotes: 0

AjayR
AjayR

Reputation: 4179

You can Use following extension (works both 1.5 and 1.6) to include custom javascript or HTML easily.

http://extensions.joomla.org/extensions/edition/custom-code-in-modules/5435

Also when you use directly may use full path like this (change the path of your .JS)

<?php
$document = &JFactory::getDocument();
$document->addScript( '/media/system/js/sample.js' );
?>

Upvotes: 0

zod
zod

Reputation: 12437

check the view source .

copy paste the path of the career.js as browser url and try to access it.

Change the path as needed.

.   ./   ../

Upvotes: 0

Related Questions