繁体   English   中英

Drupal 7覆盖自定义主题中的jquery js文件

[英]Drupal 7 override jquery js file in custom theme

是否可以重写/覆盖自定义模板脚本变量中使用的默认Drupal 7.26 jquery?

我的意思是js文件之一:

<script type="text/javascript" src="http://drupal.dev/misc/jquery.js?v=1.4.4"></script>

由自定义主题来的?

我在sites/all/MYTPL/template.php尝试了这个但是它不起作用:

$scripts['misc/jquery.js']['data'] = $base_theme . '/js/packages/jquery-2.1.0.min.js';

我想知道是否有人在不使用任何模块(例如jQuery Update情况下管理它?

===更新===

实际上,我根据@cojomojo的回答,通过一些修改,通过接受的答案帮助解决了这个问题:

function THEMEname_js_alter(&$javascript) {
    // Swap out jQuery to use an updated version of the library.
    $javascript['misc/jquery.js']['data'] = drupal_get_path('theme', 'THEMEname') . '/js/packages/jquery-2.1.0.min.js';
}

在主题的template.php文件中,在hook_js_alter使用此代码。

function [YOUR_THEME]_js_alter(&$js)
{
    $jqKey = "my-new-jquery"; // a key for new jquery file entry
    $js[$jqKey] = $js['misc/jquery.js']; // copy the default jquery settings, so you don't have to re-type them.
    $js[$jqKey]['data'] = "https://code.jquery.com/jquery-2.1.0.min.js"; // the path for new jquery file.
    $js[$jqKey]['version'] = '2.1.0'; // your jquery version.

    unset($js['misc/jquery.js']); // delete drupal's default jquery file.
}

所以你有几个选择来做到这一点。 他们来了:

您可以像这样使用hook_js_alter:

<?php
function hook_js_alter(&$javascript) {
  // Swap out jQuery to use an updated version of the library.
 $javascript['misc/jquery.js']['data'] =         drupal_get_path('module', 'jquery_update') . '/jquery.js';
}
?>

或者为了避免像2pha所提到的破坏,你可以并排运行两个版本的jquery。 jQuery已经具有与不同版本的jQuery一起运行的功能(或者,实际上,与使用$符号作为函数或变量的任何其他JavaScript库一起运行)。 这是noConflict()函数。 您可以在此处查看API页面: http//api.jquery.com/jQuery.noConflict/

要在项目中使用此功能,只需告诉浏览器您的新jQuery库,并通过自定义noConflict标识符引用它。 以下是如何在自定义主题中实现此功能的示例。

的MyTheme / page.tpl.php中

<head>
  <title><?php print $head_title; ?></title>
  <?php print $head; ?>
  <?php print $styles; ?>
  <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
  <script type="text/javascript">
    var $jq = jQuery.noConflict();
  </script>
  <?php print $scripts; ?>
</head>

另一种可能的方法是将jQuery与预处理页面交换,如下所示:

<?php
function yourModuleOrThemeName_preprocess_page(&$variables) {
  // exclude backend pages to avoid core js not working anymore
  // you could also just use a backend theme to avoid this
  if (arg(0) != 'admin' || !(arg(1) == 'add' && arg(2) == 'edit') || arg(0) != 'panels' || arg(0) != 'ctools') {
    $scripts = drupal_add_js();
    $new_jquery = array(drupal_get_path('theme', 'YOURTHEME') . '/js/jquery-1.7.1.min.js' => $scripts['core']['misc/jquery.js']);
    $scripts['core'] = array_merge($new_jquery, $scripts['core']);
    unset($scripts['core']['misc/jquery.js']);
    $variables['scripts'] = drupal_get_js('header', $scripts);
  }
}
?>

对我来说,最好的方法是并行运行两个版本的jQuery(Drupal 7默认使用的版本以及主题需要的任何版本)或者使用预处理页面交换jQuery。 hook_js_alter有可能破坏需要不同版本的jQuery的任何东西。 所有可能的方法来做你所要求的是: https//drupal.org/node/1058168

也许使用hook hook_js_alter
请记住,虽然其他模块将依赖于jQuery,如果它们使用的版本之间发生了变化,您可能会遇到问题。

暂无
暂无

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

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