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();开发者_Go百科
$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
Try this, it's working perfectly.
$document = &JFactory::getDocument();
$document->addScript('modules'.DS.'MOD_YOURMODULENAME'.DS.'javascript.js');
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.
. ./ ../
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' );
?>
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
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
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
Make sure that you have this
<jdoc:include type="head" />
after head tag in template index.php
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 :)
精彩评论