1

I used drupal 6.22 and I enabled the jQuery Update and jQuery UI modules; I used the latest version (JQuery 1.6.4 and 1.8.16) in my libraries. When I added this code in template.php (my theme) all the page worked correctly in my web site.

drupal_add_js('sites/all/libraries/jqgrid/js/jquery.js');
drupal_add_js('sites/all/libraries/jqgrid/js/jquery-ui-custom.min.js');

When I decided to use a content delivery network (CDN) some of the pages don't work correctly.

drupal_set_html_head('<script type=text/javascript src="https://www.google.com/jsapi?key=my site"></script>'); 
drupal_add_js('google.load("jquery", "1.6.4"); google.load("jqueryui", "1.8.16");'

Why is this happening?

If I use the following code, some of the pages still don't work correctly.

drupal_set_html_head('<script type=text/javascript src="https://www.google.com/jsapi?key=my site"></script>'); 
drupal_add_js('google.load("jquery", "1.6.4"); google.load("jqueryui", "1.8.16");
1
  • Read the answer from @Pierre Buyle. Using the most recent version of jQuery can cause all sorts of problems, esp with admin pages and other pages that use ajax functionality.
    – mpdonadio
    Commented Nov 9, 2011 at 11:59

5 Answers 5

3

In Drupal 6, you cannot use drupal_add_js to add external JS files. You need to use drupal_set_html_head. Thankfully, in D7 this has been fixed.

drupal_set_html_head('<script type="javascript" src="https://www.google.com/jsapi?key=my site"></script>');
drupal_add_js('google.load("jquery", "1.6.4"); google.load("jqueryui", "1.8.16");', 'inline');
3

Adding the markup to load a more recent version of jQuery from a CDN is not a good solution, it won't prevent the older version from being loaded. You will eventually run into issues when the wrong version of jQuery is used by a script expecting the other. The proper solution is to alter the path the jQuery (and jQuery UI) files before outputting the <script> markup as the jQuery update module does.

The Advanced CSS/JS Aggregation is probably an easier solution as it claims to be able to load jQuery and jQuery UI from CDNs.

Note that the Drupal 6 version of jQuery update only provides jQuery1.3.2 because some Drupal core's JavaScript scripts are not compatible with more recent versions of jQuery. For the same reason the Drupal 7 versions currently stops at jQuery 1.5.2 and jQuery UI 1.8.11. If you use more recent versions, you will eventually runs into incompatibility issues.

2
  • The third paragraph is spot on and very important to understand.
    – mpdonadio
    Commented Nov 9, 2011 at 11:58
  • AdvAgg will allow 'external' to work in D6 drupal.org/node/1279758. If using the AdvAgg JS CDN sub-module it will allow you to set the jquery version via a variable drupal.org/node/1071624#comment-4401842. Jquery UI and swfobject will also get loaded from the Google CDN.
    – mikeytown2
    Commented Dec 8, 2011 at 11:28
2

The jQuery CDN module upgrades Drupal's internal version of jQuery with one hosted on an external CDN.

2
  • The poster mentions using D6.22 and that module only has a D7 version available.
    – mpdonadio
    Commented Nov 9, 2011 at 11:55
  • This module could be probably backported to d6 Commented Nov 12, 2011 at 10:40
1

The AdvAgg sub module "AdvAgg CDN Javascript" will use Google's CDN for jQuery & jQuery UI. Be sure to change version.txt in jquery_ui to the version you want to use & set the advagg_js_cdn_jquery_update_version variable to 1.6.4

0

If you are using Drupal 7 then you can add an external js file using drupal_add_js():

drupal_add_js('http://example.com/example.js', 'external');

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.