欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Including a different jQuery version only on specific page

shiping1 的头像

jQuery update module is including this script.

<script type="text/javascript" src="/sites/all/modules/jquery_update/replace/jquery.min.js?6"></script>

I need this on every page, except a certain template file which I am using fancy box, and need a later version of jQuery.

I need to exclude jQuery update from putting the above script in the $scripts variable ONLY on page-media-photos.tpl.php.

shareimprove this question
 
up vote10down voteaccepted
+50

      正确答案  Here are your options:

  • Use jQuery Multi, as mentioned in another answer. It can be configured to load the higher version of jQuery just on that page and just for the fancy box. It won't interfere with Drupal's jQuery or with jquery_update's jQuery.
  • In Drupal 7, you can use hook_js_alter(), which will let you swap out jquery_update's version with whatever you want.
  • In Drupal 6 with jQuery Update, it provides a hook that lets you alter the JS after it's done doing its thing. You can look at the jQuery Multi 6.x code to see how it uses this hook.
  • In Drupal 6 without jQuery Update, you can do this using a preprocess_page hook. The $scripts variable will be available there, and you can change it. This can get a bit tricky. You can also see how qmulti does this. jQuery Update also does this in a similar way.

Remember that swapping to a higher jQuery version can break some things in Drupal core and contrib, which is the problem jQuery Multi solves. So if you do this make sure you're not breaking anything. The benefit here is that you just load the jQuery you need, while with jQuery Multi you'll load both.

shareimprove this answer
 
   
Note, hook_js_alter isn't available in D6 – David Thomas Feb 1 '13 at 23:31
   
Ah, thanks. I missed the 6 tag. – goron Feb 2 '13 at 0:12
2 
Changed the answer to have D6 info as well. – goron Feb 2 '13 at 0:23
   
I agree with using jQuery multi if you want to run multiple versions of jQuery. I would only recommend jquery update if you want to run the newer version for your whole site. Also, in my experience, jquery update for jquery 1.7 & 1.8 is still to buggy to use in production. – rooby Feb 2 '13 at 1:34

Drupal 7

You can call jquery_update_jquery_replace() on specific page (e.g. it in hook_library_alter()) as demonstrated in here by drupalmoff:

<?php
/**
 * Implements hook_module_implements_alter().
 */
function your_module_module_implements_alter(&$implementations, $hook){
  if( $hook == 'library_alter' ){
    $group = $implementations ['your_module'];
    unset($implementations ['your_module']);
    $implementations ['your_module'] = $group;
  }
}

/**
 * Implements hook_library_alter().
 */
function your_module_library_alter(&$javascript, $module) {
  if( $module === 'system' && current_path() == 'path/to/page' ) {
    // Make sure we inject either the minified or uncompressed version as desired.
    $min = variable_get('jquery_update_compression_type', 'min') == 'none' ? '' : '.min';
    $cdn = variable_get('jquery_update_jquery_cdn', 'none');
    $path = drupal_get_path('module', 'jquery_update');

    $version = '1.7';

    jquery_update_jquery_replace($javascript, $cdn, $path, $min, $version);
  }
}
?>

Drupal 6

My solution was to change JQUERY_UPDATE_REPLACE_PATH constant before jquery_update loads it:

<?php
/**
 * Replace jQuery files on specified paths (fix for IE 6&7 - jQuery bug: bugs.jquery.com/ticket/6498)
 */
// if (array_search($_GET['q'], array('my_page')) !== FALSE) {
if (arg(0) == 'node' && arg(1) == '123') {
  define('JQUERY_UPDATE_REPLACE_PATH', 'sites/all/libraries/jquery/1.5.2');
}
?>

So it can be added either at very beginning of the .module file (which is loaded before jquery_update, or try in settings file.

shareimprove this answer
 

Please find this code may helpful, Place this code in your template.php

function MYTHEME_preprocess_page(&$vars) {
  $scripts = drupal_add_js();
  if (in_array('page-media-photos', $vars['template_files'])) {
    unset($scripts['all']['module']['PATH_OF_JS_NEED_TO_REMOVE']);
  }
  $vars['scripts'] = drupal_get_js('header', $scripts);
}

PATH_OF_JS_NEED_TO_REMOVE give the path of js for ex: sites/all/modules/og/og.js

shareimprove this answer
 
   
function phptemplate_preprocess_page(&$vars) { $scripts = drupal_add_js(); if(in_array('page-media-photos',$vars['template_files'])) unset($scripts['all']['module']['/sites/all/modules/jquery_update/replace/jquery‌​.min.js']); $vars['scripts'] = drupal_get_js('header', $scripts); } this did not seem to work – userAgent Dec 27 '12 at 18:18
   
I upvoted but it doesn't work – userAgent Dec 28 '12 at 19:46
   
I updated the code to indicate that you need to replace MYTHEME with your theme name instead of using "phptemplate". Also, I would not recommend using this type of approach to remove a librarye that other js depends on, like jQuery, because this doesn't take into account the order that js is loaded and you could end up with jquery is undefined errors if other jQuery plugins are loaded before a version of jQuery. Also, the way jQuery update works is more complex than just unsetting a js file. – rooby Feb 2 '13 at 2:15

If you have troubles with jqueryupdate and views admin pages (for example) try to install Dev version of ctools and everything will be OK, just tested myself.

shareimprove this answer
 

There is a module jQuery multi. This module allows you to have multiple versions of jQuery run parallel on your drupal instance. It provides an alias for each instance which you can use for calling that version of jquery.

shareimprove this answer




来自  http://drupal.stackexchange.com/questions/54014/including-a-different-jquery-version-only-on-specific-page

普通分类: