1

I am working on Recruiter distribution which uses a Cloudy theme.

I have tried 2 ways to add custom javascript to my site:

1)Adding jquery code to the .info file of Cloudy theme(default theme) called cloudy.js.

2)Adding a new .js file under js/myjs.js and added this to the .info file of Cloudy theme scripts[] = js/myjs.js

However,when I view the source code of my frontpage, neither cloudy.js nor myjs.js is loaded and as a result the jquery code doesn't work.

What am I missing? Has anything to do that it is a distribution and not a drupal site? thanks

  • Have you cleared a cache after adding js file to .info? – Vishal Patil Mar 12 '15 at 11:11
  • Yep I have cleared cache multiple times – sokratis Mar 12 '15 at 11:12
  • Check if template.php has used hook_js_alter implemented & if array of js is altered. Also check html.tpl.php for $scripts variable printed or not. – Vishal Patil Mar 12 '15 at 11:14
  • Cloudy theme is not using hook_js_alter.This is however used under the Omega theme(which I think Cloudy theme inherits). Also the html.tpl.php file from Omega prints the $scripts variables. – sokratis Mar 12 '15 at 11:23
  • Double check your code is wrap in a closure. – CocoSkin Mar 12 '15 at 12:25
1

In order to allow Drupal Core to handle the JS for you. You need to use drupal_add_js() function to load the JS. This has many important aspects to loading JS into a Drupal site. If you do not use drupal_add_js() performance features in Drupal such as aggregating JS files will not work. If you are not using the aggregate feature on a production website you are missing out on one very important feature of Drupal 7.

This code goes into your template.php file within your theme. You can also use the same exact code in a custom module. You only have to change the path function and hook names.

/**
* Implements hook_preprocess_html().
*/

function yourthemename_preprocess_html(&$variables, $hook) {   
   $path = drupal_get_path('theme','yourthemename') . 'any/extra/path/needed/myjs.js';
   drupal_add_js($path);
}
1

***EDITED****

How about if you add the js into the HEAD tag .

This goes into your default Theme that is being used and you need to add this hook to the template.php file ; if it doesn't already exist. Remember to Clear you cache afterwards also.

/**
* hook_preprocess_html
*/

function yourtheme_preprocess_html(&$variables, $hook) {

   drupal_add_js(drupal_get_path('theme', 'yourtheme') . '/js/common.js');

}

  • Hi,thanks for the reply.Which theme and which file shall I add this?I am a newbie with theming – sokratis Mar 12 '15 at 11:37
  • apologies I've edited to make clearer. hopefully! – Deejay Mar 12 '15 at 12:05
  • $JSPath =$base_url.'examplefolderpath/scripts/some.js'; is missing a file semicolon. It should be $JSPath =$base_url.'/examplefolderpath/scripts/some.js'; – Potney Switters Mar 12 '15 at 12:30
  • This worked,now I can see my script under the HEAD of my pages.However the script doesn't run,maybe its not correct.I will look into it,guys thanks for the help! – sokratis Mar 12 '15 at 12:53
  • While this does work it is not the proper way to add JS in Drupal. You should always use drupal_add_js() to get scripts into your pages. This allows other modules to alter your JS such as for performance (LABjs, Minify, AdvAgg, etc) and it allows Drupal core to be able to aggregate you JS. – Brady Mar 12 '15 at 21:20
0

The answers is already been provide. This is another solution if you wish to add JS from a Drupal module.

You can used the JS injector.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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