Topic for discussion - Loading JS's in modules

Events happening in the community are now at Drupal community events on www.drupal.org.
perignon's picture

I got a topic to talk about. We could even start the discussion here as well as at the next meetup.

I been fairly successful writing my own modules except for ONE thing. Loading an existing javascript using the API, drupal_add_js(). So far I have not got it to work and I've had to revert to putting the JS into a template file and loading it which is wrong, but works...

Each time I have tried this it causes all kinds of havoc on the site, admin menu stops working and all my flexsliders.

Comments

Have you tried a different JS

miche's picture

Have you tried a different JS file that you know is syntactically correct? Like just an "alert('w00t')". I am wondering if it is the JS code, not the method of adding it.


Michelle Lauer
michellelauer.com

Oh ya.. Scripts work fine

perignon's picture

Oh ya.. Scripts work fine crammed in a tpl file. Cause havoc if I use drupal_add_js().

From which function/hook are

miche's picture

From which function/hook are you adding the JS? What is the drupal_add_js() line you are adding?


Michelle Lauer
michellelauer.com

I'm doing this in a custom

perignon's picture

I'm doing this in a custom module.

drupal_add_js('path-to-js-file')

I've also added options per the api documentation to drupal_add_js to change the location, weight, blabla of the JS I'm adding but always the same results.

The one I ran up on is adding the twitter JS to run the twitter feed. Most recently - last night - I was adding the JS for Facebook like buttons, G+ like buttons, and Twitter buttons (all the counting widgets) and none of those work either. They do work when crammed in a template file though.

Here is the JS for the

perignon's picture

Here is the JS for the Twitter feed:

https://gist.github.com/3794890

Try this.... Change your JS

miche's picture

Try this....

  1. Change your JS file to ONLY have what is INSIDE the second set of script tags.
  2. Place the following two lines in whatever function/hook you are using. Replace 'MY_MODULE' & 'MY_JS' with what is appropriate.

    drupal_add_js('https://widgets.twimg.com/j/2/widget.js', array('type' => 'external', 'scope' => 'footer'));
    drupal_add_js(drupal_get_path('module', 'MY_MODULE').'/MY_JS.js', array('type' => 'file', 'scope' => 'footer'));


Michelle Lauer
michellelauer.com

Hrm... didn't think about

perignon's picture

Hrm... didn't think about tearing it apart. I'll try that now.

You know what I been

perignon's picture

You know what I been missing.... 'type' => 'file'

And it loaded without messing anything else up!

Although I quickly found out the second part of the JS for that twitter thing is a rendering JS so it has to be in the block TPL file so that it will render the content to allow it to be displayed in a block.

All it took was bouncing this

perignon's picture

All it took was bouncing this off some people. Just learned a lot in like a few minutes. Awesome!

New Hampshire

Group categories

Regional Audience

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week