0

I am using below code in my custom module "my_module".

    function my_module_theme(){
        return array(
                'my_module_javascript'=>array('arguments'=>array(),),
        );
 }

 function my_module_init(){
        theme('my_module_javascript');
 }

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

After I enable the module page source is showing the javascript file without .js extension. like src="modulepath/my_module?lrmjns" Am I doing anything wrong? why is it skipping .js?

1 Answer 1

2

What version you are using? And are you sure the my_module.js is exist?

I think the best way is using hook_library to add js and css for your module:

function my_module_library() {
  $libraries['my-module'] = array(
    'title' => 'My custom module library',
    'version' => '1.0',
    'js' => array(
      drupal_get_path('module', 'my_module') . '/my_module.js' => array(),
    ),
  );
  return $libraries;
}

function my_module_init() {
  drupal_add_library('my_module', 'my-module');
}
2
  • Sorry my mistake, there is one more file being added with correct extension because of the typo in my .info file. By the way, is there any difference between these methods? drupal_add_js, drupal_add_library and script[] ='mymodule.js' in info file? Sep 16, 2011 at 3:43
  • drupal_add_js is the low level to add a Javascript file. script[]='mymodule.js' will always add Javascript file to Drupal, can't lazy load. hook_library allow your module and other module can add Javascript, CSS file which version support in easy way and when you need to upgrade your Javascript source (and keep current source), only one modify need. Sep 16, 2011 at 6:50

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.