8

I'm trying to add some javascript to my template.php file on a Drupal 7 site. I'd like the javascript to load on a specific set of pages, based on the url. For example, I'd like the script to load on:

mysite.com/blog/page1

mysite.com/blog/page2

but not on:

mysite.com

or mysite.com/blog

or mysite.com/about

I'm using

function mytheme_preprocess_html(&$variables) {
  $theme_path = path_to_theme();
  $path = drupal_get_path_alias();
  if($path == 'blog/page1') {
      drupal_add_js($theme_path . '/js/example.js');
    }
}

to load the script on that specific page, but is there any way to use a url argument or wildcard or something so that all internal blog pages (i.e. blog/page1, blog/page2, blog/page3) will load the script?

Thanks!

7

The drupal_match_path() function should do the trick:

$path = drupal_get_path_alias();
$pattern = 'blog/*';

if (drupal_match_path($path, $pattern)) {
  drupal_add_js($theme_path . '/js/example.js');
}
0

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.