【发布时间】:2011-11-30 18:09:56
【问题描述】:
我有一个模块,我在其中添加了 js
if($_GET['q'] == 'workpage') {
drupal_add_js(drupal_get_path('module', 'sb_carousel').'/js/slide.js');
}
问题是将其添加到 .info 文件中定义的 jqueru 之上。我看到有一个“权重”属性如何让我的 js 在 js 文件的底部??
【问题讨论】:
我有一个模块,我在其中添加了 js
if($_GET['q'] == 'workpage') {
drupal_add_js(drupal_get_path('module', 'sb_carousel').'/js/slide.js');
}
问题是将其添加到 .info 文件中定义的 jqueru 之上。我看到有一个“权重”属性如何让我的 js 在 js 文件的底部??
【问题讨论】:
您可以通过将$options 数组传递给drupal_add_js() 来设置权重:
$file = drupal_get_path('module', 'sb_carousel').'/js/slide.js';
$options = array(
'weight' => 1000, // High number to push this file to the bottom of the list
'scope' => 'footer' // This will output the JS file in the footer scope, so at the end of the document
);
drupal_add_js($file, $options);
使用weight 和scope,您应该能够确保您的JS 文件是HTML 中最后输出的。
更新
只是想一想,您提到您的 jQuery 文件是从 .info 文件加载的……没有必要这样做,默认情况下,jQuery 已经添加到 Drupal 中。您可以使用jQuery Update module 升级到 jQuery 1.5,但目前没有(官方)支持任何更高版本。
如果您正在加载第二个 jQuery 文件,这可能是首先导致问题的原因,除非您正确使用 jQuery.noConflict(),即使那样您仍然可能遇到问题。
【讨论】: