JQuery and prototype.js, how to save jQuery as $?

So, I am working on a site that uses both jQuery and prototype.js, however they conflict.

I researched a fair bit and found that the only way to solve this problem is to use

<script>
 jQuery.noConflict();

 // Use jQuery via jQuery(...)
 jQuery(document).ready(function(){
   jQuery(\"div\").hide();
 });

 // Use Prototype with $(...), etc.
 $('someid').hide();

However, I do not want to change $ to jQuery, as this means that I will need to edit the fair bit of the previously written code. Is there a way to stop their conflict and leave jQuery as $?

+5
source share
5 answers

Instead of using a ready-made document, you can create a closure (at the end body), for example:

(function($) {
  //jQuery stuff
  $('.elem') // $ refers to jQuery
})(jQuery);

If I understand your question correctly

+9
source

, , . , - :

var j$ = jQuery.noConflict();

jQuery $ j$. , /.

+6
( function($){
   // $('jquery here')
})(jQuery);

jquery $.

+2

, jQuery. , jQuery :

(my_old_stuff = function($){
   $.my_existing_function(){
       alert('i allready exist');
   };
)(jQuery);

jQuery.my_existing_function();

.

: $ jQuery, , .

0

, . . js $.

From jQuery documentation

<!-- Loading jQuery before other libraries. -->
<script src="jquery.js"></script>
<script src="prototype.js"></script>
<script>

// Use full jQuery function name to reference jQuery.
jQuery( document ).ready(function() {
    jQuery( "div" ).hide();
});

// Use the $ variable as defined in prototype.js
window.onload = function() {
    var mainDiv = $( "main" );
};

</script>

Here are all the options available.

https://learn.jquery.com/using-jquery-core/avoid-conflicts-other-libraries/

0
source

All Articles