Drupal – 如何在head标签中添加简单的jQuery和jQuery UI?

就个人而言,我不喜欢使用jQuery Update模块,1。看起来像很多不必要的开销,而且2.它只允许更新到jQuery 1.8,这是旧的,如果你问的话,这个模块需要自己更新我!

因此,在加载任何其他js文件之前,我需要在Drupal 7中将以下URL添加到我的head标签中。

Google CDN jQueryurl://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js

Google CDN jQuery UIurl://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js

我已经尝试将它放入theme.info文件中,但它来自CDN,所以这不起作用,因为theme.info中的js链接到主题为根的相对路径。

那么,如何在Drupal 7中正常条件下的任何其他脚本之前将其添加到head标签中?

我做了推荐的编辑,由于某种原因,不再为该主题加载CSS文件。 我在这里使用媒体响应主题: https : //drupal.org/project/media-responsive-theme

这是我的theme.php文件的样子:

 'utf-8' ); } /** * Insert themed breadcrumb page navigation at top of the node content. */ function media_responsive_theme_breadcrumb($variables) { $breadcrumb = $variables['breadcrumb']; if (!empty($breadcrumb)) { // Use CSS to hide titile .element-invisible. $output = '

' . t('You are here') . '

'; // comment below line to hide current page to breadcrumb $breadcrumb[] = drupal_get_title(); $output .= ''; return $output; } } /** * Override or insert variables into the page template. */ function media_responsive_theme_preprocess_page(&$vars) { $html5jqueryui = array( '#tag' => 'script', '#attributes' => array( // Set up an array of attributes inside the tag 'src' => 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js', ), '#prefix' => '', '#suffix' => '', ); drupal_add_html_head($html5jqueryui, 'html5jqueryui'); $html5jquery = array( '#tag' => 'script', '#attributes' => array( // Set up an array of attributes inside the tag 'src' => 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', ), '#prefix' => '', '#suffix' => '', ); drupal_add_html_head($html5jquery, 'html5jquery'); if (isset($vars['main_menu'])) { $vars['main_menu'] = theme('links__system_main_menu', array( 'links' => $vars['main_menu'], 'attributes' => array( 'class' => array('links', 'main-menu', 'clearfix'), ), 'heading' => array( 'text' => t('Main menu'), 'level' => 'h2', 'class' => array('element-invisible'), ) )); } else { $vars['main_menu'] = FALSE; } if (isset($vars['secondary_menu'])) { $vars['secondary_menu'] = theme('links__system_secondary_menu', array( 'links' => $vars['secondary_menu'], 'attributes' => array( 'class' => array('links', 'secondary-menu', 'clearfix'), ), 'heading' => array( 'text' => t('Secondary menu'), 'level' => 'h2', 'class' => array('element-invisible'), ) )); } else { $vars['secondary_menu'] = FALSE; } } function media_responsive_theme_js_alter(&$js) { // var_dump($js); unset($js['misc/jquery.once.js']); unset($js['misc/jquery.js']); unset($js['misc/ui/jquery.ui.core.min.js']); } /** * Duplicate of theme_menu_local_tasks() but adds clearfix to tabs. */ function media_responsive_theme_menu_local_tasks(&$variables) { $output = ''; if (!empty($variables['primary'])) { $variables['primary']['#prefix'] = '

' . t('Primary tabs') . '

'; $variables['primary']['#prefix'] .= '
    '; $variables['primary']['#suffix'] = '
'; $output .= drupal_render($variables['primary']); } if (!empty($variables['secondary'])) { $variables['secondary']['#prefix'] = '

' . t('Secondary tabs') . '

'; $variables['secondary']['#prefix'] .= '
    '; $variables['secondary']['#suffix'] = '
'; $output .= drupal_render($variables['secondary']); } return $output; } /** * Override or insert variables into the node template. */ function media_responsive_theme_preprocess_node(&$variables) { $node = $variables['node']; if ($variables['view_mode'] == 'full' && node_is_page($variables['node'])) { $variables['classes_array'][] = 'node-full'; } } function media_responsive_theme_page_alter($page) { // $viewport = array( '#type' => 'html_tag', '#tag' => 'meta', '#attributes' => array( 'name' => 'viewport', 'content' => 'width=device-width' ) ); drupal_add_html_head($viewport, 'viewport'); }

在页面头部的实际源代码中,它现在看起来像这样:

          Testing Site | Just a test site @import url("http://localhost:8082/modules/system/system.base.css?myw3hn"); @import url("http://localhost:8082/modules/system/system.menus.css?myw3hn"); @import url("http://localhost:8082/modules/system/system.messages.css?myw3hn"); @import url("http://localhost:8082/modules/system/system.theme.css?myw3hn"); @import url("http://localhost:8082/modules/comment/comment.css?myw3hn"); @import url("http://localhost:8082/sites/all/modules/date/date_api/date.css?myw3hn"); @import url("http://localhost:8082/sites/all/modules/date/date_popup/themes/datepicker.1.7.css?myw3hn"); @import url("http://localhost:8082/sites/all/modules/date/date_repeat_field/date_repeat_field.css?myw3hn"); @import url("http://localhost:8082/modules/field/theme/field.css?myw3hn"); @import url("http://localhost:8082/modules/node/node.css?myw3hn"); @import url("http://localhost:8082/modules/search/search.css?myw3hn"); @import url("http://localhost:8082/modules/user/user.css?myw3hn"); @import url("http://localhost:8082/sites/all/modules/views_slideshow_xtra/views_slideshow_xtra_example/css/views_slideshow_xtra_example.css?myw3hn"); @import url("http://localhost:8082/sites/all/modules/views_slideshow_xtra/views_slideshow_xtra_overlay/css/views_slideshow_xtra_overlay.css?myw3hn"); @import url("http://localhost:8082/sites/all/modules/views/css/views.css?myw3hn"); @import url("http://localhost:8082/sites/all/modules/admin_menu_dropdown/admin_menu_dropdown.css?myw3hn"); @import url("http://localhost:8082/sites/all/modules/ckeditor/ckeditor.css?myw3hn"); @import url("http://localhost:8082/profiles/acquia/modules/ctools/css/ctools.css?myw3hn"); @import url("http://localhost:8082/sites/all/libraries/fancybox/source/jquery.fancybox.css?myw3hn"); @import url("http://localhost:8082/sites/all/libraries/fancybox/source/helpers/jquery.fancybox-thumbs.css?myw3hn"); @import url("http://localhost:8082/sites/all/libraries/fancybox/source/helpers/jquery.fancybox-buttons.css?myw3hn"); @import url("http://localhost:8082/sites/all/modules/views_slideshow_xtra/views_slideshow_xtra.css?myw3hn"); @import url("http://localhost:8082/sites/all/themes/media-responsive-theme/style.css?myw3hn"); @import url("http://localhost:8082/sites/all/themes/media-responsive-theme/media.css?myw3hn"); @import url("http://localhost:8082/sites/default/files/fontyourface/font.css?myw3hn");                         

我想也许过早调用jquery,或者在$ js中还有其他变量未设置?

我在Chrome中查看错误时填充的JS错误列表如下:

 Uncaught ReferenceError: Drupal is not defined views_slideshow_xtra_overlay.js:6 Uncaught ReferenceError: Drupal is not defined fb.js?myw3hn:25 Uncaught ReferenceError: Drupal is not defined fb_connect.js:17 Uncaught ReferenceError: Drupal is not defined fancybox.js:7 Uncaught ReferenceError: Drupal is not defined views_slideshow_xtra.js:6 Uncaught ReferenceError: Drupal is not defined masonry.js:8 Uncaught ReferenceError: Drupal is not defined localhost/:1372 Uncaught ReferenceError: Drupal is not defined localhost/:1535 Uncaught ReferenceError: Drupal is not defined admin_menu_dropdown.js:3 

基本上,“Drupal没有定义”。 如何定义?

 Add the following function in your theme template.php file. function YOURTHEMENAME_preprocess_html(&$vars) { $html5jquery = array( '#tag' => 'script', '#attributes' => array( // Set up an array of attributes inside the tag 'src' => 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', ), '#prefix' => '', '#suffix' => '', ); drupal_add_html_head($html5jquery, 'html5jquery'); $html5jqueryui = array( '#tag' => 'script', '#attributes' => array( // Set up an array of attributes inside the tag 'src' => 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js', ), '#prefix' => '', '#suffix' => '', ); drupal_add_html_head($html5jqueryui, 'html5jqueryui'); }