2

I am trying to set particular javascript files for a specific node page by getting the path alias. like this:

$path=drupal_get_path_alias();  
if($path == 'about-us')
  drupal_add_js(drupal_get_path('theme', 'mytheme') . "/page-download.js");

which it doesnt work. But when i add the particular javascript file to my .info file it works. Also in other pages i will have to unset some of the javascript files if the above does not work but still unsetting javascripts does not work.

unset($vars['js']['all']['page-download.js']);

1 Answer 1

4

As far as adding the JS file goes it probably depends where you're calling that code from. If you're calling it directly in a template file it's probably happening too late in the page build.

A hook_preprocess_page() implementation would probably be a good place for it (as would hook_init() and probably a bunch of others). You can also use request_uri() to check the current path without incurring a lookup for the alias.

function MYMOUDLE_preprocess_page(&$vars) {  
  if (request_uri() == '/about-us') {
    drupal_add_js(drupal_get_path('theme', 'mytheme') . "/page-download.js");
  }
}

If for whatever reason you can't get that to work, you can remove a JS file by implementing hook_js_alter():

function MYMODULE_js_alter(&$js) {
  if ($some_condition) {
    $js_path = drupal_get_path('theme', 'mytheme') . "/page-download.js";
    if (isset($js[$js_path])) {
      unset($js[$js_path]);
    }
  }
}
8
  • Yes thank you! I am calling that code from mytheme_preprocess page in the template.php file. So i will have to create a module to unset the unwanted javascript files in specific path aliases?
    – paranod
    Commented Aug 8, 2012 at 12:49
  • If I remember rightly you should be able to use hook_js_alter() in your theme, but I can't find anything on the docs page that says that you can. Best way would be to try...if not, a custom module it is :)
    – Clive
    Commented Aug 8, 2012 at 12:56
  • Implemented it now as a hook_js_alter(&$javascript) with no luck. I was just wondering if the parameter of the function is correct. Because your code above is using MYMODULE_js_alter(&$js) { instead of hook_js_alter(&$javascript) that drupal suggests (api.drupal.org/api/drupal/modules!system!system.api.php/…. So i guess i will have to implemented it as a module.
    – paranod
    Commented Aug 8, 2012 at 13:10
  • $js/$javascript is just the name of the variable to be used in the scope of that function. As long as you're using the same name in the rest of the function (e.g. unset($javascript[$js_path]);) there's absolutely no difference. Remember to clear the caches after adding the new hook or it won't be picked up. If you want to make sure the hook is running, add drupal_set_message('test'); to the function. If you see the message, it's running, and the problem will probably be that Drupal can't find the JS file at the path you're giving it
    – Clive
    Commented Aug 8, 2012 at 13:13
  • yes i have it all to $javascript variable. I cleared the cache and no message appeared. I tried echo to the path as well with no luck:( I dont think my javascript file path is wrong since i have the file inside /sites/all/themes/ninesixty so the path is correct. Maybe a time of thinking for a module?
    – paranod
    Commented Aug 8, 2012 at 13:24

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.