简体   繁体   English

Drupal 6 - 将Javascript添加到视图中

[英]Drupal 6 - Add Javascript to a View

I want to add a Jquery plugin to a view. 我想在视图中添加一个Jquery插件。 However, I don't want to add via the .info file, as I don't want it on every page. 但是,我不想通过.info文件添加,因为我不想在每个页面上添加它。 I also don't want to add it to the header of the view or create a view.tpl.php file. 我也不想将它添加到视图的标题或创建view.tpl.php文件。

This page shows how to add Java Script to a Node via template.php. 页面显示如何通过template.php将Java脚本添加到节点。 Is there anyway to do something similar to add Java Script to a View via the template.php file? 反正有没有做类似的事情通过template.php文件将Java脚本添加到视图?

Here is an example for Daniel Wehner's answer. 这是Daniel Wehner回答的一个例子。 It assumes your view is called 'blog', and your display id is 'page_1'. 它假设您的视图称为“博客”,您的显示ID为“page_1”。 This code goes into template.php. 此代码进入template.php。 Remember to clear the template cache after adding new functions to template.php. 记住在向template.php添加新函数后清除模板缓存。

/**
 * Implements hook_preprocess_views_view().
 */
function THEMENAME_preprocess_views_view(&$vars) {
  $view = $vars['view'];
  // Load the blog.js javascript file when showing the Blog view's page display.
  if ($view->name == 'blog' && $view->current_display == 'page_1') {
    global $theme;
    drupal_add_js(drupal_get_path('theme', $theme) . '/js/blog.js', 'theme', 'footer', FALSE, TRUE, FALSE);
  }
}

您可以使用hook hook_preprocess_views_view

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM