I want to add a file.js to my site pages.

First I did this by way of mytheme.info file
scripts[screen][] = myjsfile.js
and it worked well.

Now I want to do it by way of a custom module.
So I wrote the custom_module.module file this very simple way:

<?php
drupal_add_js(drupal_get_path('module', 'mymodule') . '/myjsfile.js');

The result is as follows:
In the source code of the page, as expected, I can see the line
<script type="text/javascript" src="http://localhost/drupal-7.0/sites/all/modules/customodules/mymodule/myjsfile.js?lj1r1b"></script>
but the line
<script type="text/javascript" src="http://localhost/drupal-7.0/misc/jquery.js?v=1.4.4"></script>
which is normally loaded in every Drupal page, has disappeared, so the result is that it does not work.

I have in mind that something in my custom_module.module file code is missing.
Thanks in advance for your help.

Comments

sijans’s picture

Version: 7.x-dev » 7.0
Status: Fixed » Active

<?php
drupal_add_js(drupal_get_path('module', 'mymodule') . '/myjsfile.js');

It is right method to include javascript file through module.I think there is something wrong with your drupal installation.

Try this
drupal_add_js("sites/all/customodules/mymodule/myjsfile.js");

Thnks
Sijan Shrestha

tstoeckler’s picture

Version: 7.0 » 7.x-dev
Status: Active » Fixed

There are two ways to add JavaScript to modules in Drupal 7.
If you want to always load a JavaScript file (which seems to be the case for you), you can load them from the info file, just like you can now for themes:
scripts[] = myfile.js
(Not scripts[screen][] = myfile.js; the "screen" part is for CSS only)
If you want to load the JavaScript file only on certain pages, you can do this differently. In that case you would have to elaborate on your use-case in order for me to help you how to do that.

Also: Would you mind writing a documentation page for your problem? That would probably help a lot of people that are having the same problem as you. Maybe create a child page of Creating Drupal 7.x modules. If you do that please link the page here for reference. Thanks in advance!

Marking "fixed" for now, please reopen if problems persist.

gilbertdelyon’s picture

Version: 7.0 » 7.x-dev
Status: Active » Fixed

Thanks for your answers. In the meantime I found a way to make it running.
The wrong way:
I wrote the custom_module.module file this very simple (may be too basic) way:

<?php
drupal_add_js(drupal_get_path('module', 'mymodule') . '/myjsfile.js');

The result was that:
- "myjsfile.js" was well included in the site pages as expected
- "jquery.js" was not included. So "mysjfile.js" was useless.

The right way:
I embeded the drupal_add_js() function into a hook_init() function, so the custom_module.module file now looks like this:

<?php
mymodule_init(){
drupal_add_js(drupal_get_path('module', 'mymodule') . '/myjsfile.js');
}

And now myjsfile.js and jquery.js are well loaded and well running. it works!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Chris Charlton’s picture

Issue summary: View changes

It's generally not good (performance) practice to use hook_init() when another hook (any other hook) would be more appropriate.