You are here

遇到drupal_add_js函数加载js失败的问题

dreamour 的头像
Submitted by dreamour on 星期四, 2011-01-06 01:39

葛老师,在模块的specialloupan.module里面有函数如下:

function theme_specialloupan_quote($feature) {
 
  drupal_add_css(drupal_get_path('module', 'specialloupan') . '/specialloupan.css');
  drupal_add_js(drupal_get_path('module', 'specialloupan') . '/specialloupan.js');
 
  $opts = array( 'absolute' => true );
  $json_url = url('specialloupan.json/'.$feature, $opts);
  drupal_add_js(array('specialloupan' => array('json_url' => $json_url) ),'setting');
 
  $output = drupal_get_path('module', 'specialloupan') . '/specialloupan.js';
  return $output;
}

虽然,函数是执行了的(因为已经显示出来字符串“sites/all/modules/specialloupan/specialloupan.js ” ),但不知何故,drupal_add_css和drupal_add_js都没有正确执行?(把后面的三条语句注释掉也没有用)

论坛:

dreamour 的头像

这个函数包括模块都是从原来正常无误的模块复制得来的。

我把模块名字和相应的info文件,以及各个程序和css文件里面该修改的地方修改了。

dreamour 的头像

//这是另外一个模块的函数代码,是没有问题的

function theme_philquotes_quote($nid) {
  $full_path = drupal_get_path('module', 'philquotes')
    .'/philquotes.css';
 
  drupal_add_css($full_path);
  drupal_add_js(drupal_get_path('module', 'philquotes')
    .'/philquotes.js');
 
  $opts = array( 'absolute' => true );
  $json_url = url('philquotes.json/'.$nid, $opts);
//  drupal_add_js('var json_path = "' . $json_url . '";', 'inline');
  drupal_add_js(array('philquotes' => array('json_url' => $json_url) ),'setting');
  $output = '';
  return $output;
}
 

dreamour 的头像

缓存清空后,也一样没有能加载js.

我是在首页page-front.tpl.php里面调用模块specialloupan,

  <?php
    $block = module_invoke('specialloupan', 'block', 'view', 0, array(), 'smallunit');
    print $block['content'];
  ?>

模块应该正确调用了,函数theme_specialloupan_quote也执行了,里面的 $output 已经正确显示了,但drupal_add_css和drupal_add_js就没有正确执行,百思不解。

我把函数theme_specialloupan_quote里的$output修改如下

  $output = $feature;
  return $output;

可以在首页相应位置正确地显示字符串smallunit,说明参数传递也正常。