2010-08-06 20 views
4

我需要在我的Drupal中使用最新版本的jQuery。Drupal和最新的jQuery

jQuery更新模塊是不夠的,因爲它不會更新jquery到其最新版本。

無論如何要在前端使用最新版本的jquery,並在後端使用包含drupal的版本?

我應該使用主題模板文件嗎?這是否會導致問題?

謝謝

回答

3

Drupal在每個使用JavaScript的頁面中包含jQuery庫。使用比一個Drupal附帶的jQuery更新版本的唯一方法是安裝jQuery Update,它還提供了一個腳本,用於與使用舊版本庫的腳本兼容。 jQuery的更新模塊的更新:

  • 的Drupal 5將jQuery 1.2.6
  • 的Drupal 6將jQuery 1.3.2
  • Drupal 7的將jQuery 1.5.2和jQuery UI 1.8.11

如果您要簡單地替換/ misc中包含的jQuery庫,那麼Drupal腳本將停止工作(我試過這樣做,那就是結果)。 在Drupal將該庫加載到/ misc之後加載更新的版本也會導致問題。

爲了完成這些信息,有一個模塊附帶了自己版本的jQuery庫,然後綁定到$ui;這樣,其他代碼仍然會使用默認的jQuery庫,而您的模塊/主題將使用更新的庫。 只需將該模塊加載了自己的jQuery的版本,然後執行以下JavaScript行:

window.$ui = jQuery.noConflict(true); 

使用相同的方法,你就可以加載最新版本的jQuery的,且不會與現有模塊的任何衝突。 如果您使用從其他人開發的模塊並使用使用$的Javascript代碼,則此係統不起作用;它適用於您在您的網站上運行的自定義模塊/主題。

+2

jQuery更新通常也是我推薦的,但不幸的是,最新的穩定版本隨附於1.2.6版本,最新的alpha版本隨附1.3.2。對於真正需要jQuery 1.4.x的人來說,目前還沒有完美的解決方案。 – marcvangend 2010-08-07 07:29:39

+0

@marcvangend:就是這一點。我完全同意,謝謝。 – aneuryzm 2010-08-08 09:21:32

0

您是否對前端和後端使用不同的主題?您可以在前端的template.php中添加一些代碼到themename_preprocess_page()函數中,以替換該主題中的jquery文件。

+0

是我使用不同的主題。確實..但我想知道這是否安全。例如..假設我正在使用需要jQuery的lightbox模塊:它是使用舊的jquery開發的..可能會導致衝突嗎? – aneuryzm 2010-08-06 20:22:16

+0

問題是jQuery不是用於主題,而是來自Drupal核心代碼。 – kiamlaluno 2010-08-07 06:22:05

+0

當然,kiamlaluno是對的。然而,像CCK和Views這樣的Drupal核心和模塊主要在管理頁面上使用jQuery,而在前端頁面上則少得多。因此,如果您可以確保您的管理主題使用1.2.6,而您的前端主題使用1.4.2,則需要解決的問題更少。 – marcvangend 2010-08-07 07:25:24

0

1)下載jquery-1.4.2.js和jquery-1.4.2.min.js。
2)將它們複製到jquery_update /替換文件夾。
3)編輯jquery_update.module:
替換功能jquery_update_jquery_path(){}與

function jquery_update_jquery_path() { 
    $jquery_file = preg_match('/(admin|edit|add)/', request_uri()) ? 
     array('none' => 'jquery.js',  'min' => 'jquery.min.js') : 
     array('none' => 'jquery-1.4.2.js', 'min' => 'jquery-1.4.2.min.js'); 
    return JQUERY_UPDATE_REPLACE_PATH . '/' . $jquery_file[variable_get('jquery_update_compression_type', 'min')]; 
} 
+1

這會給我帶來一些使用jquery前端模塊的問題。查看其他答案/評論。 – aneuryzm 2010-08-08 09:22:41

0

using jQuery.noConflict()什麼?我不知道Drupal模塊在jQuery 1.2/1.3中有任何具體的例子,但是在1.4中打破了這個測試,但在Drupal加載jQuery 1.2/1.3之後,表面上加載了jQuery 1.4(所以,將它添加到你的主題中)並使用var jNew = jQuery.noConflict()會將$()返回給Drupal大部分期望的jQuery,並允許您使用jNew()來執行您需要執行的jQuery 1.4操作。

+0

我會試試這個解決方案。所以,看看我是否理解得很清楚:當我使用jNew()選擇我的項目時,我正在使用新的jquery而不是舊的! – aneuryzm 2010-08-08 16:23:21

+0

這是正確的。 Drupal及其模塊仍然會使用'$()'或'jQuery()'來選擇事物,並且從不懷疑還有另一個版本的jQuery也被加載。 – 2010-08-08 17:01:24

2

是的,您可以使用jQuery更新(如果您需要jQuery 1.8使用開發版本)以及類似於模塊中的代碼來切換前端和後端的版本。

/** 
* Implements hook_module_implements_alter(). 
*/ 
function mymodule_site_module_implements_alter(&$implementations, $hook) { 
    if ($hook == 'library_alter') { 
    if(isset($implementations['jquery_update'])) { 
     // Move jquery update to the end. This will make sure our hook_library_alter 
     // is always called before the jquery_update. 
     $jquery_update = $implementations['jquery_update']; 
     unset($implementations['jquery_update']); 
     $implementations['jquery_update'] = $jquery_update; 
    } 
    } 
} 

/** 
* Implements hook_library_alter(). 
*/ 
function mymodule_site_library_alter(&$libraries, $module) { 
    // If it is the admin theme all we want to do is change the global $conf 
    // variable so when jquery_update runs right after us it will use 1.5. 
    // We are not using path_is_admin(current_path()) because some admin path can use 
    // the frontend theme like node edit page 
    global $theme_key; 
    if (variable_get('admin_theme') == $theme_key) { 
    // Modifying global $conf variable, can be dangerous. Be carefull. 
    global $conf; 
    $conf['jquery_update_jquery_version'] = '1.5'; 
    } 
} 

我寫了一篇關於它的博客文章http://antistatique.net/blog/2013/01/04/drupal-use-a-different-jquerys-version-for-the-frontend-and-backend/

+0

感謝您一直在尋找的東西,不需要補丁。 – chadpeppers 2013-04-25 19:53:47

0

對於前端我做了一些「壞」,但它的工作。

我包括了最新的jQuery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
<script type="text/javascript">$.browser = {msie: false}; // fix for scripts that use that property</script> 

禁用Drupal的jQuery的

<?php print str_replace(
    '<script type="text/javascript" src="/misc/jquery.js', 
    '<script no-drupal-jquery="', 
    $scripts 
); 
?> 

,我得到

<script no-drupal-jquery="?W"></script> 
<script ...