0

I have a module that will provide a dropdown when creating a new block. Selecting either none, option 1 or option 2 will add a corresponding class to that block. That class will be targeted by a small (>4kb) jquery file. The file needs to be able to access every page since blocks can be put anywhere on the site.

I have seen multiple methods of adding JS files to websites, including editing the theme .info file. What is the best way to accomplish what I am trying to do? If the info file is the best way to go, is there a way to programmatically add the JS file to the info file?

0

You can add the file to a theme's or module's .info file using scripts[]:

scripts[] = js/my-script.js

The path should be relative to your module root.

See also Writing theme .info files and Writing module .info files.

  • I know I can do that, but I want to be able to have it completely taken care of upon the module being enabled. Any Joe Schmoe should be able to enable this module, and not have to modify the .info file. Is there a way I can do this without having to manually add the script? – mattcoker Mar 5 '14 at 18:59
  • You are not adding any script. With that line you tell drupal your module needs that file in every page. – Astrayus Mar 5 '14 at 19:57
  • @mattcoker That's exactly what that scripts[] line does. When the module is enabled, Drupal reads its .info file and finds the scripts[] line, and then automatically includes the script on every single page. – Jesse Pinho Mar 5 '14 at 20:42
  • My apologies. I thought you meant specifically adding that to my theme .info file. I totally understand now. – mattcoker Mar 5 '14 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.