3

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

simekadam
  • 7,334
  • 11
  • 56
  • 79

8 Answers8

7

Try this, it's working perfectly.

$document = &JFactory::getDocument();
$document->addScript('modules'.DS.'MOD_YOURMODULENAME'.DS.'javascript.js');
Botz3000
  • 39,020
  • 8
  • 103
  • 127
Yesceeohhh
  • 116
  • 1
  • 5
  • In newer versions of Joomla, you can use forward slash `/` instead of Joomla Directory Separator `DS`, for example: `$document->addScript('modules/MOD_YOURMODULENAME/javascript.js');` – Farahmand Mar 31 '14 at 15:03
0

Make sure that you have this

<jdoc:include type="head" />

after head tag in template index.php

Irfan
  • 7,029
  • 3
  • 42
  • 57
0

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 :)

Nirav Zaveri
  • 687
  • 1
  • 9
  • 28
0

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.

.   ./   ../
zod
  • 12,092
  • 24
  • 70
  • 106
  • I tried to access directly to the file and it showed properly..But when using addScript, the script is missing:( – simekadam Jun 01 '11 at 05:56
  • I think that in case of bad url I will find in result code a script tag with src attr which would be set to the value I set?? – simekadam Jun 01 '11 at 06:30
0

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' );
?>
AjayR
  • 4,169
  • 4
  • 44
  • 78
  • I tried move it into this folder, without effect..Too tried to incluce another js file from this folder.. I have Joomla 1.5, could it help? So I will try the extension you wrote about – simekadam Jun 01 '11 at 05:58
  • So the module is wokring somehow weird:DWhen I write there a js code, it dissappears when rendering, but regular text displays properly. I tried both settings for clean html and js.. – simekadam Jun 01 '11 at 06:19
  • im having the same issue, im going to try this tonight – Drewdin Feb 06 '12 at 21:50
0

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

simekadam
  • 7,334
  • 11
  • 56
  • 79
  • i think we can have different templates for home page and inner page.is it possible – zod Jun 01 '11 at 13:35
0

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

eyeonu
  • 211
  • 8
  • 15
0

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

simekadam
  • 7,334
  • 11
  • 56
  • 79