- Home
- Blogs
- George Boobyer's blog
- Drupal_add_js - Tip: Checking for javascript already added to page
Drupal_add_js - Tip: Checking for javascript already added to page
We are working on a series of Facebook modules for Drupal at the moment. These enable us to use Drupal to serve facebook applications and integrate the site(s) we are developing tightly with Facebook. We are developing them as a set of submodules (more on that later...)
One of the issues we have faced is that javascript needed for opengraph integration may be added by more than one module (custom and contrib).
Using drupal_add_js() in several modules.
Like:
<?phpdrupal_add_js('http://connect.facebook.net/en_US/all.js', array('type' => 'external', 'scope' => 'footer', 'weight' => 1)) ;$fbscript = 'FB.init({appId :' . $appid . ',status : true, cookie : true, xfbml : true});';drupal_add_js($fbscript,array('type' => 'inline', 'scope' => 'footer', 'weight' => 5)) ;?>
I decided that what was needed was to check to see if the javascript has been added already before adding it - and came across this great tip by vasi1186
You can do that with drupal_add_js(NULL, NULL, NULL). This will return you the array with the js files that were added.
Great!
Of course module weight is important here as you can't help ones that run after yours.
There is always hook_js_alter() should you want to dedupe after everyone has added theirs.
So that's adding javascript - but I quite often get asked how to remove javascript from the scripts array too.
The best way is to use hook_js_alter.
This enables you to target specific script entries added by drupal_add_js:
In this example I remove a load of javascipt files not required by the maintenance page and colorbox pages for anonymous users
function mytheme_js_alter(&$js) {
global $user ;
if ((variable_get('maintenance_mode', 0) && (!$user->uid)) ||
((isset($_GET['template']) && $_GET['template'] == 'colorbox'))) {
unset($js[drupal_get_path('module','overlay').'/overlay-parent.js']);
unset($js[drupal_get_path('module','views_slideshow').'/js/views_slideshow.js']);
unset($js[drupal_get_path('libraries','jquery.cycle').'/jquery.cycle.all.min.js']);
unset($js[drupal_get_path('module','views_slideshow_cycle').'/js/views_slideshow_cycle.js']);
unset($js[drupal_get_path('module','admin_menu').'/admin_menu.js']);
unset($js[drupal_get_path('module','jcarousel').'/js/jquery.jcarousel.min.js']);
unset($js[drupal_get_path('module','jcarousel').'/js/jcarousel.js']);
unset($js[drupal_get_path('module','field_group').'field_group.js']);
unset($js[drupal_get_path('module','toolbar').'toolbar.js']);
unset($js[drupal_get_path('module','views').'js/jquery.ui.dialog.patch.js']);
}
View the source of your pages to see what gets included and target those you don' want.
Contact Details
Blue-Bag Ltd
- info [at] blue-bag.com
- Telephone: 0843 2894522
- Blue-Bag HQ:
The Garage, Manor Farm
Chilcompton, Radstock
Somerset, BA3 4HP, United Kingdom - Telephone: (+44) 01761 411542
- Blue-Bag Brighton:
Unit 35 Level 6 North, New England House
New England Street, Brighton
BN1 4GH United Kingdom - Telephone: (+44) 07944 938204
- VAT GB 748125034
- UK Company Reg: 3932829