Drupal 6 javascript in module block for front page -
i'm trying create simple image slider on front page of drupal website. wrote module basic outline follows
<?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 module, admin/block page $block[0]["info"] = t("slider"); break; case "view": // generate content blocks module $block_content = ""; $block_content .= "hello"; //query projects $icons = db_query("select * {alumni_frontpage_projects}"); //initiation arrays each table column: title, description, url, icon link $links = array(); $title = array(); $description = array(); $url = array(); //generate arrays 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 , randomly choose 1 $res = count($links)-1; $seed = rand(0,$res); //generate html , javascript of slider //check 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; }
now, fine. generate block , can place wherever want. great. i use javascript in block can pass arrays , use onclick events slide through images arrays queried. found out have pass variables javascript , i'll have identify javascript file want use in module.
//add variable javascript drupal_add_js(array('slider_settings' => array('variable_name' => $variable_name)), 'setting'); //add javascript file module drupal_add_js(drupal_get_path('module', 'slider') .'/slider.js');<br>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
put these in hook_init nothing happens!!!!! tested see basic alerts. if put hard code _block, such as:
$block_content .= ' <script type="text/javascript"> var x x = 50; document.write(x); //prints value of x </script>';
then see '50' prints.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if try pass variable hard coded script. not work either. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if try write 50 through previous code, separate file called slider.js in same folder using "drupal_add_js(drupal_get_path('module', 'slider') .'/slider.js');", not work either (even though i'm not passing variable)
heck going on!! possible i'm missing important core drupal files? there way trouble shoot further?
thanks!
drupal_add_js should work in hook_init().first check whether hook function correctly named modulename_init() or not.
Comments
Post a Comment