我想在我的drupal网站的首页上创建一个简单的图像滑块。我写了一个模块,其基本轮廓如下Drupal 6 javascript in front page的模块块
<?php
function slider_init(){
drupal_add_js(drupal_get_path('module', 'slider') .'/slider.js');
}
function slider_block($op = 'list', $delta = 0, $edit = array()) {
$block = array();
switch ($op) {
case "list":
// Generate listing of blocks from this module, for the admin/block page
$block[0]["info"] = t("slider");
break;
case "view":
// Generate content for blocks from this module
$block_content = "";
$block_content .= "hello";
//Query for the projects
$icons = db_query("SELECT * FROM {alumni_frontpage_projects}");
//Initiation arrays from each table column: title, description, url, icon link
$links = array();
$title = array();
$description = array();
$url = array();
//Generate arrays from each table column: title, description, url, icon link
while($icon_data = db_fetch_array($icons)){
$links[] = $icon_data['slider_image_location'];
$title[] = $icon_data['title'];
$description[]= $icon_data['description'];
$url[] = $icon_data['url'];
}
//Count elements in array and randomly choose one
$res = count($links)-1;
$seed = rand(0,$res);
//Generate HTML and Javascript of Slider
//Check that content isn't empty
if ($block_content == "") {
$block_content = t("Sorry No Content");
}
$block["content"] = $block_content;
break;
case "save":
break;
case "configure":
break;
}
return $block;
}
现在,一切都很好。我生成一个块,我可以把它放在任何我想要的地方。大。但我真的很想在这个块中使用javascript,这样我就可以传递数组并使用onclick事件来滑过我查询的数组中的一些图像。所以我发现,我将不得不将变量传递到JavaScript,我将不得不识别我想在模块中使用的JavaScript文件。
//add variable to the Javascript
drupal_add_js(array('slider_settings' => array('variable_name' => $variable_name)), 'setting');
//add Javascript file to module
drupal_add_js(drupal_get_path('module', 'slider') .'/slider.js');<br>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我把这些在hook_init但没有任何反应! !!!!我测试过看到基本警报。如果我将硬代码放入_block中,例如:
$block_content .= '
<script type="text/javascript">
var x
x = 50;
document.write(x); //prints the value of x
</script>';
然后我看到'50'打印。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
如果我尝试将变量传递给硬编码的脚本。那也行不通。 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
如果我尝试通过前面的代码写50,但是从一个单独的文件名为slider.js,它在同一个文件夹中使用“drupal_add_js(drupal_get_path('module','slider')。'/ slider.js');”,这也不起作用(即使我不是传递一个变量)
那么究竟是怎么回事!是否有可能我错过了一些重要的核心Drupal文件?有没有办法来进一步解决这个问题?
谢谢!
好的,我会证实这一点。 – ingrid