[Drupal] How to add the js file and js code block in Drupal

Drupal 6:

 代码

  // This will add a JS file to your head (specifically the $scripts variable in page.tpl.php)
  drupal_add_js(drupal_get_path('module', 'my_module'. '/my_module.js');

  
// This add inline JS to the head of the document
  drupal_add_js('alert("Hello!")', 'inline');

  
// This will add variables in the Drupal.settings object
  drupal_add_js(array('my_module' => array('my_setting' => 'this_value')), 'setting');
  
  
//add an external JS file to your site.
  //You can not add <script XXXX  />, but you have to add as <script XXX></script>,
  //because the first one will cause some problem (webkit-base browser)

  drupal_set_html_head('<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>');

 

 

 Drupal 7:

代码
  drupal_add_js('misc/collapse.js');
  drupal_add_js(
'misc/collapse.js', 'file');
  drupal_add_js(
'jQuery(document).ready(function () { alert("Hello!"); });', 'inline');
  drupal_add_js(
'jQuery(document).ready(function () { alert("Hello!"); });',
    
array('type' => 'inline', 'scope' => 'footer', 'weight' => 5)
  );
  drupal_add_js(
'http://example.com/example.js', 'external');
  drupal_add_js(
array('myModule' => array('key' => 'value')), 'setting');

 

 

Details: http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_add_js 

posted @ 2010-12-14 15:48  DavidHHuan  阅读(541)  评论(0编辑  收藏  举报