Prototype was my first love when I was introduced to the amazing world of the JavaScript libraries. This library uses the $()
shortcut in an almost completely different way than jQuery. The problem with jQuery is that both libraries share the same shortcut name, thus leading to many conflicts when they have to coexist on the same site. WordPress is an excellent example of this problem. Fortunately, jQuery provides the noConflict()
method. The question now is: should we always try to avoid such conflicts? Answer: only when it's necessary.
Prototype and jQuery may cause problems only when they're loaded together. When this happens, both libraries are registered as properties of the global window
object.
Their namespaces clash only when it comes to the $()
alias. In fact, Prototype has a completely different way to interact with page elements than jQuery.
We should try to avoid conflicts only if both libraries share the same environment. If Prototype is not present, why bother? For that reason, we should test first whether Prototype has been loaded or not.
Here's a simple test:
var _$; if(typeof $ !== 'undefined' && typeof Prototype !== 'undefined') { _$ = jQuery.noConflict(); }
$
must be present and at the same time also Prototype
must be defined. Only in this case we can use the noConflict()
method before working with jQuery. If Prototype is not loaded, it's pointless to use a method designed to avoid conflicts when there are no conflicts.
If you work with the WordPress backend, you'll probably notice that Prototype and jQuery can actually work very well together. In this case, conflict is not a synonym of problem.