Add Js

Submitted by shiv on Sun, 11/20/2016 - 11:30

Add a JS file to head from Module

  <?php /*  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');
?> 

Add inline JS to the head

  <?php /*  This will add inline JS to the head of the document  */ 
  drupal_add_js('alert("Hello!")', 'inline');
?> 

Add a JS file to head from Theme

  <?php /*  This will add a JS file to your head (specifically the $scripts variable in page.tpl.php)  */ 
drupal_add_js(drupal_get_path('theme', 'my_theme_name') . 'my_theme.js');
?> 

Add CDN/External(cross domain) JS

  <?php /* Comment goes here */ 
drupal_add_js('//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js', array(
  'type' => 'external',
  'scope' => 'header',
  'group' => JS_THEME,
  'every_page' => TRUE,
  'weight' => -1,
));
?> 

Drupal 7 Add JS at footer

  <?php /* D7 Add External/CDN JS at footer */ 
drupal_add_js('//code.jquery.com/jquery-2.1.3.min.js', array('scope'=>'footer'));
?> 
  <?php /* D7 Add JS at footer */ 
drupal_add_js(drupal_get_path('theme', 'beginning').'/js/bootstrap.min.js', array('scope'=>'footer'));
?> 

Drupal 6 Add JS at footer

  <?php /*  Drupal 6 add external/CDN JS at footer */ 
drupal_add_js('//code.jquery.com/jquery-2.1.3.min.js','theme','footer');
?> 
  <?php /*  Drupal 6 add JS at footer  */ 
drupal_add_js(drupal_get_path('theme', 'shiv').'/js/bootstrap.min.js','theme','footer');
?> 

Sample blackbox block

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas non porta odio, id mattis erat.

Nam sed tincidunt eros. Donec a venenatis odio, vitae commodo sem. Vivamus ullamcorper pellentesque erat, in adipiscing tortor scelerisque sit amet. Nulla pulvinar urna magna. Vivamus a ipsum lorem. Etiam adipiscing pulvinar lorem.