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

function profile2_form_user_register_form_alter(&amp;$form, &amp;$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()
This is the right &amp; simple way to include any JS file in Drupal 7.


Create a folder named i.e. js, inside your module folder.
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);

Open (modulename).info file &amp; add:

scripts[] = js/(modulename).js
Clear Cache &amp; Run.