7

I am using this, but I don't understand why it's not working.

if ($_SERVER['REQUEST_URI'] == '/node/1') { 
  drupal_add_js(drupal_get_path('theme', 'sanskrit').'/sites/all/themes/sanskrit/scripts/jquery-1.4.3.min.js', 'inline'); 
  drupal_add_js('/sites/all/themes/sanskrit/scripts/scripts/jquery.nivo.slider.pack.js');   
} 

What I want to do is adding specific JavaScript files when the user is viewing the front page.

17
+50

This should do it:

function MYTHEME_preprocess_page(&$vars) {
  if (drupal_is_front_page()) {
    drupal_add_js(drupal_get_path('theme', 'MYTHEME') . '/MYSCRIPTS/myscript.js');
    $vars['scripts'] = drupal_get_js();
  }
}

FYI: You'd use drupal_get_js in case any modules are also using hook_preprocess_page and setting new scripts themselves.

Also, I wouldn't just add a newer version of jQuery in willy nilly. Try either jQuery Update or jQuery Multi

  • hey i am getting error in this line function MYTHEME_preprocess_page(&$vars) { what should i do – Jaiffable Jun 14 '12 at 13:09
  • Are you getting Undefined function error? I guess it should be drupal_is_front_page() like my answer below. – ninjascorner Jun 14 '12 at 13:14
  • Whoops...I've edited my answer. That's one of those functions I ALWAYS type wrong. Thanks for pointing it out! – Chapabu Jun 14 '12 at 13:33
4

Alternately: The JavaScript Libraries module. It doesn't let you limit the script to the front page, but is certainly much easier than writing a module just to load a script.

3

If you are working with a module, you put it in hook_init like this:

function mymodule_init() {
  if(drupal_is_front_page()) {
    drupal_add_js(drupal_get_path('theme', 'sanskrit') . '/sites/all/themes/sanskrit/scripts/jquery-1.4.3.min.js'); 
    drupal_add_js(drupal_get_path('theme', 'sanskrit') . '/sites/all/themes/sanskrit/scripts/scripts/jquery.nivo.slider.pack.js');
  }
}

If you want to put it in preprocess_page:

function yourTheme_preprocess_page(&$vars) {
  if(drupal_is_front_page()) {
     drupal_add_js(drupal_get_path('theme', 'sanskrit') . '/sites/all/themes/sanskrit/scripts/jquery-1.4.3.min.js'); 
     drupal_add_js(drupal_get_path('theme', 'sanskrit') . '/sites/all/themes/sanskrit/scripts/scripts/jquery.nivo.slider.pack.js');
     $vars['scripts'] = drupal_get_js();
  }
}

Or you can put it in your theme.info file if you are building your own theme.

scripts[] = scripts/jquery.nivo.slider.pack.js
2

The best and simplest means to accomplish adding javascript to a front page is via context and context add assets. Context and context add assets are exportable as well and you can create a feature to be used later on.

More about context add assets:

This module will add a reaction within context to add theme, module, or custom folder files to your page(s).

Ever wanted to quickly add a css file to a theme? Include a js file on only one page (or section?)

Make using multiple CSS files a breeze with this module and harness the power of Context in your theme!

1

It is not working because you are concatenating the string returned from drupal_get_path('theme', 'sanskrit') with '/sites/all/themes/sanskrit/scripts/jquery-1.4.3.min.js'; supposing that you didn't install Drupal in a sub-directory of the server root directory, the filename you are passing to drupal_add_js() is sites/all/themes/sanskrit/bbbsites/all/themes/sanskrit/scripts/jquery-1.4.3.min.js.

The code you should use is the following one.

if ($_SERVER['REQUEST_URI'] == '/node/1') {
  $theme_path = drupal_get_path('theme', 'sanskrit'); 
  drupal_add_js($theme_path . '/scripts/jquery-1.4.3.min.js', 'inline'); 
  drupal_add_js($theme_path . '/scripts/jquery.nivo.slider.pack.js');   
} 

As others pointed out, to check if the user is viewing the front page, you just call drupal_is_front_page().

Paths like sites/all/themes should be never hard coded in a theme/module code, as a theme could be installed in sites/<domain.name>/themes/<theme.name>, and a module could be installed in sites/<domain.name>/modules/<module.name>. Suppose that the Drupal installation is used for the domain blog.example.com, the Views module could be installed in sites/blog.example.com/modules/views, and a theme like the Whitebull theme could be installed in sites/blog.example.com/themes/whitebull.

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.