aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Mix jQuery with other libraries? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Mix jQuery with other libraries? " Watch "Mix jQuery with other libraries? " New topic
Author

Mix jQuery with other libraries?

Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
Hi,

I am more a ocasional java script developer. As I understood the debate so far, jQuery is very good for a certain task (traversal of DOM).
Now if in my team, there is relatively much experience with Prototype, is it advisable (or even possible) to mix the 2 libraries?
That means more specifically: Using prototype for ajax stuff and jQuery just for the complexer dom-access bits? Or qualifies mixing prototype and jQuery as a particularly bad idea and jQuery should be used separated from prototype for projects, where more advanced dom access is expected?

regards
Axel
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15292
    
    6

I'm currently using both in a project. Not because I want to but because I had to. Since both libraries use the $ function the only thing you need to do is make sure you tell jQuery to release control of that function by calling:

jQuery.noConflict()

somewhere before all your other scripts run. That will tell jQuery to ignore the $ function so that prototype (or some other library) can use it. Then you just need to use jQuery.foo instead of $.foo for all your jQuery functions.

Personally, I prefer jQuery to prototype for everything; AJAX, DOM traversal. I just really like jQuery's approach to the problem and its extensibility. Granted, I haven't peaked that the very latest prototype release (1.6 I think) but as of 1.5 I believe JQuery to be a much better library.


GenRocket - Experts at Building Test Data
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

Indeed, jQuery goes out of its way to make sure that it plays well with others.

Unlike most other libraries, its global name incursion is limited to two identifiers: jQuery and $. And as Gregg reported, the latter can be unbound in a supported fashion through a call to $.noConflict(). Now that's friendly!

Also like Gregg, I also find jQuery's Ajax support to be more intuitive and less wordy than Prototype's and use jQuery exclusively for my personal and client projects. In my day job, we use Prototype for a bunch of things and so, like with your project, the two libraries are sometimes used on the same page.

Because of jQuery's non-invasiveness, this is never a problem.
[ January 15, 2008: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Yehuda Katz
Author
Greenhorn

Joined: Jan 14, 2008
Posts: 21
Bear makes a good point. It would be entirely feasible to use jQuery's Ajax support only while using Prototype's class support alongside it, because jQuery's components are very tightly namespaced:



Keep in mind that if you want to use jQuery's global callbacks for Ajax (such as setting callback functions for *every* Ajax call), though, you'll need to use jQuery exclusively for making Ajax calls.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mix jQuery with other libraries?
 
Similar Threads
Prototype and JQuery
Adding more controls at runtime
AJAX XMLHttpRequestObject Question?
Trigger servlet with javascript function
Difference among DWR,jQuery,JSON and DOJO