I am new to Drupal and I am trying to create a module that requires js. I have successfully created the module. I have successfully created the script.js file. Now I tried to include other js files $.include('script2.js') but it does not work. It should be that easy, right?

Then I tried a different method. The template.php

function _phptemplate_variables($hook, $vars) {
  if ($hook == 'page') {
    // Add the script.js file   
	 
	drupal_add_js(path_to_theme() .'/script2.js');

    // Reload the scripts variable to include the added file
    $vars['scripts'] = drupal_get_js();
	return $vars;
  }
   return array();
}

and the page.tpl.php method

print _phptemplate_variables($hook, $vars);

but it does not work either, it only adds the follwoing lines to my source:

<script type="text/javascript"> </script>
Array

What's up with that!?

Thanks for any suggestions,
Tiffany

Comments

jrowny’s picture

You don't print the hook directly, it's just returning an array. Do you have print $head; somewhere in your page.tpl? That's where drupal will output required JS includes.

jrowny’s picture

also, if you're developing a module, the javascript file should be in the module directory, not in the theme. Something like: drupal_add_js(drupal_get_path('module', 'mymodule') .'/my_js_file.js') and this shouldn't be called from template because that's part of the theme. The module should be independent of a theme.

If you're simply looking to include a JS file on your site, simply edit your theme's .info file.

fischert’s picture

I know that module under my theme may not be best practices here but I am not asking for best practices at this point I am just learning. Maybe when I have more time to develop my own modules for submission to Drupal I will revisit this : )

Please tell me how should I edit it .info file? Just type the file name script2.js?

fischert’s picture

Incorporating js into the module is so simple!

fischert’s picture

So very simple