Search code examples
javascriptjqueryprototypejs

Mixing jQuery and Prototype while preserving $ == jQuery


The common solution when mixing e.g. prototype and jQuery is using var $j = jQuery.noConflict(); to restore the prototype function $ and then using $j to access jQuery.

However, since prototype will be completely replaced with jQuery at some point, I'd prefer to use $ for jQuery and e.g. $p for prototype. Is this possible?

I know I could wrap my jQuery code in (function($) { /* my code */ })(jQuery); but I guess it'd be cleaner if I could get rid of the global $ var pointing to a prototype function.


Solution

  • Prototype doesn't have a mechanism to give up control of the $ function like jQuery does. Your best bet, as you note, is to wrap your jQuery code in a function that has $ locally defined to jQuery. Once you have all of the prototype code removed, you can remove the wrappers.