Adding external java script file in Joomla component

200 views
Skip to first unread message

foysal foysal

unread,
Mar 3, 2013, 1:01:48 PM3/3/13
to joomla-de...@googlegroups.com
I am trying to add external JavaScript file in this way.


$doc = JFactory::getDocument();
$doc->addStyleSheet(JURI::base().'components/com_mouse/css/mystyle.css'); $doc->addScriptDeclaration(JURI::base().'components/com_mouse/js/jquery.min-1.5.2.js');
$doc->addScriptDeclaration(JURI::base().'components/com_mouse/js/compo_js.js');

and below code is in my compo_js.js file


  $(document).ready(function(){
    $(".delete_class").click(function()
    {
      alert('Yes');
    });
  });


But it is not working.

Thanks

Foysal

Don

unread,
Mar 3, 2013, 2:24:55 PM3/3/13
to joomla-de...@googlegroups.com
Make sure another jquery isn't being loaded. Also, if moo tools is being loaded it could be causing conflict.  Lastly, since you're wrapping your code in the jquery ready, do it like this:

jQuery(document).ready(function ($) {
    // your code
});

Doing it that way prevents $ conflicts with moo tools by aliasing the $ to jQuery within your block of code. 

Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Marco Dings

unread,
Mar 4, 2013, 4:37:31 AM3/4/13
to joomla-de...@googlegroups.com
In addition to Donalds reply I would suggest to use some standardised jquery install. There are several options in the Jed. I've settled using the strapper lib from akeeba. It is installed with akeebas extensions, but you can bundle it with your own. It gives you a name spaced jquery and bootstrabshould you need it.

foysal foysal

unread,
Mar 4, 2013, 4:54:09 AM3/4/13
to joomla-de...@googlegroups.com
Dear Don,
Thanks for your reply. My codes are working if I embed them in default.php(template) file.But this is not working if I use external files which are in js folder. I used jQuery instead of $. But it is not working also.

Thanks

Foysal

s@m

unread,
Mar 4, 2013, 3:01:51 AM3/4/13
to joomla-de...@googlegroups.com
$document =& JFactory::
$document->addScript(JURI::base() .
'components/com_students/assets/script/jquery-1.8.2.js');
add to extrnal java script i think work your script

Mario Pro

unread,
Mar 4, 2013, 8:47:35 AM3/4/13
to joomla-de...@googlegroups.com
$doc->addScriptDeclaration($content, $type = 'text/javascript') adds a script to the page, while $doc->addScript($url, $type = "text/javascript", $defer = false, $async = false); adds a linked script. This is what you should use - $doc->addScript(JURI::base().'components/com_mouse/js/compo_js.js');

You can use the addScriptDeclaration() method but then you should assign $content your full script. Ex: $content = "(function ($) { // all your other js stuff here }).(jQuery)"; This is sometimes useful but be careful with quoting. 

Don

unread,
Mar 4, 2013, 10:11:34 AM3/4/13
to joomla-de...@googlegroups.com
Good catch there Mario, I didn't even see that! 



Sent from my iPhone

foysal foysal

unread,
Mar 4, 2013, 11:12:53 AM3/4/13
to joomla-de...@googlegroups.com
Thank you soooooooooooooo much Mario. It is working now.

Foysal

Reply all
Reply to author
Forward
0 new messages