将JavaScript添加到多个特定的网址


8

我正在尝试在Drupal 7网站上的template.php文件中添加一些javascript。我希望javascript根据网址在一组特定的页面上加载。例如,我想加载脚本:

mysite.com/blog/page1

mysite.com/blog/page2

但不在:

mysite.com

或mysite.com/blog

或mysite.com/about

我在用着

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');
    }
}

在该特定页面上加载脚本,但是有什么方法可以使用url参数或通配符之类的东西,以便所有内部博客页面(即blog / page1,blog / page2,blog / page3)都可以加载脚本?

谢谢!

Answers:


7

drupal_match_path()功能应该可以解决问题:

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

if (drupal_match_path($path, $pattern)) {
  drupal_add_js($theme_path . '/js/example.js');
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.