1

I have some functions in template.php file that inside some of them, some js files must be included to the template.

Now, I want to check at first whether a specific js file [for example jquery.min.js] is included before or not. (in order to avoid) duplication.

Is there any way to check this issue in drupal 6??

2 Answers 2

6

A. You should not be including jQuery manually in Drupal. Also, jQuery is always loaded on every page. If you need to include a different version of jQuery than the version that is included in Drupal core, you should utilize the jQuery Update module.

B. Secondly, you should be adding your javascript files using the drupal_add_js() function. Utilizing this function will take care of a lot of potential issues involved in managing many javascript files across many pages on your site.

C. drupal_add_js() returns an array of all javascript files. You could load a javascript file conditionally based on the presence of another javascript file, like this:

  $scripts = drupal_add_js();

  if(!isset($scripts['path/to/script.js'])) {
    drupal_add_js('path/to/script.js');
  }

D. Be sure to thoroughly read the Drupal.org documentation about using javascript in Drupal. This will answer many of your questions and solve many of your issues regarding javascript in Drupal.

7
  • At first thanks a lot for your guidance, About jquery_update module, unfortunately this module only supports jquery 1.3.x in drupal 6.x - I have to use drupal 6 and my plugins need jquery 1.6.x +, So I need to include additional jquery library with noConflict function.
    – Mehrdad201
    Apr 30, 2012 at 18:11
  • the 6.x-2.x-dev version of jQuery Update supports up to jQuery 1.7.
    – sheena_d
    Apr 30, 2012 at 18:43
  • but , it is dev version and it may have some bugs... , what is your idea ?
    – Mehrdad201
    May 1, 2012 at 7:49
  • I tested jquery_update dev version. unfortunately when I set engine on 1.7 the table drag functions (in block or other sections) don't work while when it is 1.3 table drag works fine. it seems it s better for me to use drupal_add_js with noConflict
    – Mehrdad201
    May 1, 2012 at 9:47
  • You can try doing it manually, but updating jQuery from the version that is included in Drupal core is always going to cause bugs with the javascript written for Drupal core. jQuery Update compensates for those bugs. The table drag bug is a know issue and a patch currently exists to correct it at drupal.org/node/893538
    – sheena_d
    May 1, 2012 at 12:22
0

You could use drupal_get_js() to get all the JS files thats included through Drupal. You will get an array of all the files which should make the checking pretty easy, you probably still will need to do some reg ex to figure out if the specific file is there or not.

1
  • Can you explain more? Can you give me an example ?
    – Mehrdad201
    Apr 30, 2012 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.