3

I want to include a custom JavaScript file in a module using the following code.

function profile2_form_user_register_form_alter(&$form, &$form_state) {
  drupal_add_js('http://localhost/prf2.js', 'external');
}

I then add scripts[] = js/prf2.js in the .info file, but it throws this error.

Warning: file_get_contents(js/prf2.js): failed to open stream: No such file or directory in drupal_build_js_cache()

6
  • drupal_add_js('localhost/prf2.js','external'); Is "localhost" really the appropriate domain? Consider what happens when the client tries to load this JavaScript.
    – Letharion
    Commented Oct 10, 2014 at 11:58
  • 1
    You are almost certainly going about this incorrectly since you are hacking the profile2 module rather than adding the javascript from your own custom module.
    – Letharion
    Commented Oct 10, 2014 at 11:59
  • If am I add this inside profile2.module then how to access the js file. Please clarify your above code.
    – Nishant
    Commented Oct 10, 2014 at 12:09
  • 1) I suggest we clarify something first. There is probably no need to modify (or hack, as it's often called) the profile2 module, so don't. 2) My code is 4 lines long, even counting the line );. If you don't understand it, you need to be more precise about what you need clarified.
    – Letharion
    Commented Oct 10, 2014 at 12:26
  • @Letharion Passing the second parameter as a 'type' string is actually fine, it's only if you pass an associated array you need to use 'type' => 'external'
    – Clive
    Commented Oct 10, 2014 at 12:31

3 Answers 3

19

This is the right & simple way to include any JS file in Drupal 7.

  1. Create a folder named i.e. js, inside your module folder.
  2. Inside that folder place (modulename).js file.

    Note: Drupal JS code has a specific format as follows:

    (function ($) {
      $(document).ready(function(){
        alert("Hello world!");  //here we can add our JS code
      });
    })(jQuery);
    
  3. Open (modulename).info file & add:

    scripts[] = js/(modulename).js

  4. Clear Cache & Run.

1
  • Not that this only works for Drupal 7. From Drupal 8 or later it will not work. Commented May 23 at 10:24
1

There is a lot of documentation about how to include a JS file into a custom module or into your theme in general. Here is the official article: https://www.drupal.org/node/304255

There are plenty of examples and you can inspire yourself with it.

PS: Don't hardcode the path of your JS file as you did in your code. Use the drupal_get_path function.

7
  • <?php drupal_add_js(drupal_get_path('module', 'profile2') .'/prf2.js'); ?> .info file => scripts[] = js/prf2.js Error occured
    – Nishant
    Commented Oct 10, 2014 at 11:54
  • May u pls tell me I have to add script[]=js/prf2.js is compulsory..
    – Nishant
    Commented Oct 10, 2014 at 11:57
  • If you do the drupal_add_js, you doesn't have to do it in the .info file.
    – Djouuuuh
    Commented Oct 10, 2014 at 12:02
  • Depending on your situation, you can choose to add your JS file in your theme, in your module .info file, in a module's hook, in a form...
    – Djouuuuh
    Commented Oct 10, 2014 at 12:03
  • My condition is I have a module i.e. profile2 & I have added some JS code to create condition between fields. My JS file is put inside js folder i.e. profile2.js. So in profile2.module I want to include JS. Then which condition is good for me.
    – Nishant
    Commented Oct 10, 2014 at 12:07
0

Here is two place where you can add JavaScript: In a theme or in a module.

1. Theme

If you want to add JavaScript in theme simply go in your_theme.info file and add your file path for example if your theme directory is:

mytheme\
 \js\my_theme.js
 \my_theme.info

So write down:

scripts[] = js/my_theme.js

Under your my_theme.info clear cache from admin and that should be work

2. Module

I you want to add js in module then

my_module
my_module\js\my_module.js
my_module.module

In my_module.module call hook:

function my_module_init(){
    drupal_add_js(drupal_get_path('module', 'my_module').'/js/my_module.js');
}

Above code add JavaScript once module enable.

2
  • function profile2_init(){ drupal_add_js(drupal_get_path('module', 'profile2').'/js/profile2.js'); } -------add this in my profile2.module
    – Nishant
    Commented Oct 10, 2014 at 12:05
  • scripts[] = js/profile2.js----- add this in my profile2.info.. Its not working
    – Nishant
    Commented Oct 10, 2014 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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