Hi.

I need to use prototype.js in one of my pages, but I know it can't be used together with jQuery. I know there is a workaround (by using a modified version of prototype) but I prefer another solution.

I noticed that jQuery is not included in all pages, but only when it's invoked by a module, and in my Home Page (where I want to use prototype) jQuery is not included. But I want to be sure...so, there's a way to avoid loading jQuery only on my homepage?

Thank you.

Comments

kourge’s picture

There is actually another workaround to this. Drupal ships with jQuery 1.0. However, jQuery 1.1 ships with a noConflict() feature. Replace Drupal's copy of jQuery to v1.1 and then execute the following:
jQuery.noConflict();
For safety, please also include the jQuery Compat-1.0 plugin as well. jQuery will play nicely with Prototype. The reason not to get rid of jQuery is because Drupal's own JavaScript functions (such as collapsible fieldsets, inline file uploading) rely on jQuery.

When you do jQuery.noConflict(), this reveals another problem: all core JavaScript functions of Drupal will stop working because they directly rely on the $ namespace, and now that $ belongs to Prototype, they won't work anymore. There are two ways to deal with this:
1. Port Drupal's JavaScript to Prototype. (Doesn't sound like fun.)
2. Wrap Drupal's JavaScript around an anonymous function:

(function($) {
// code
})(jQuery);

This way, the $ namespace will only work on that block of JavaScript code. You also won't need to break other functionality just to use Prototype. Keep in mind that if you install other modules that use jQuery, you'll have to wrap their code too. Some modules, like Fivestar, already do this, but most do not.

ymmatt’s picture

For everyone else that will reference this post: as long as you use drupal_add_js() you won't need to use jQuery.NoConflict(). The documentation for this can be found on their site

The reason for this is that, in my experience, jquery will always be included first and your library (S/P?) will be called afterwards and override the $ function. This is the quote from their page that states this:

If you include jQuery before other libraries, you may use "jQuery" when you do some work with jQuery, and the "$" is also the shortcut for the other library. There is no need for overriding the $-function by calling "jQuery.noConflict()".