Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drupal 7 add_js Weight

I have a module and in it I add the js with

if($_GET['q'] == 'workpage') {
drupal_add_js(drupal_get_path('module', 'sb_carousel').'/js/slide.js');
}

problem is it adds it above jqueru which is defined in the .info file. I see there is a 'weight' property how do I get my js at the bottom of the js files??

like image 763
LeBlaireau Avatar asked Apr 16 '26 18:04

LeBlaireau


1 Answers

You can set the weight by passing an $options array into drupal_add_js():

$file = drupal_get_path('module', 'sb_carousel').'/js/slide.js';
$options = array(
  'weight' => 1000, // High number to push this file to the bottom of the list
  'scope' => 'footer' // This will output the JS file in the footer scope, so at the end of the document
);

drupal_add_js($file, $options);

Using weight and scope you should be able to ensure that your JS file is the very last outputted in the HTML.

UPDATE

Just a thought, you mention that your jQuery file is loaded in from an .info file...there's no need to do that, jQuery is already added in Drupal by default. You can upgrade to jQuery 1.5 with the jQuery Update module but currently there's no (official) support for any later versions.

If you're loading in a second jQuery file it might be what's causing the problem in the first place, unless you use jQuery.noConflict() properly and even then you might still have problems.

like image 197
Clive Avatar answered Apr 18 '26 07:04

Clive



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!