I have a field which is used in a nodeblock, and only appears on a few select pages. This however will vary throughout the lifetime of the website so I've been looking for a way to insert the Javascript this field requires only when it's present. Additionally, the field can appear more than once on any given page.
Looking at drupal_add_js
, I can't see that it provides a way to only include JS once. Using this I was hoping that I could just call it in the field--field-name.tpl.php
file for that field but as it occurs multiple times, the JS is included more than once.
I've looked at the module JS Injector, but that's just page specific and just calls the aforementioned function. This wouldn't help me because every time a new block is added somewhere, the list of pages including the JS would also need to be updated.
Is there a way I can include a JS file just once in Drupal?
static
keyword? If not put that in on google, it's ideal for solving your problemstatic $done; if(!$done) { drupal_add_js(); $done = true; }
?