How to use $ for jQuery inside domready when prototype uses $ outside?

I cannot remove the prototype from the JSF framework (RichFaces 3.3.3). And if I try noConflict and try to take on $, it will break my application infrastructure because it is closely related to the prototype.

So there is a way I can do this:

jQuery(function() {
    /*
        some code that within this domready function 
        allows me to use $() within this function
        and not interfere with $ being used for prototype
        outside?
    */
});
+2
source share
4 answers

Yes, it is already passed as the first parameter to your handler ready, just use:

jQuery(function($) { 
  $("selector").doSomething();
});
//$ is still prototype here
+7
source

In general, you can write var $ = jQuery;to replace a character $within a single function.

.

+1

by taking this link as a link, you can do something like this:

jQuery(function($) { // like Nick Craver
});

and call the functions that are needed for jQuery:

var yourFunction = function(){
   var $ = this;
};

yourFunction.call(jQuery);

...

var yourFunction = (function($){

   return function() {

       // $ -> jQuery

   };

})(jQuery);

...

var yourFunction = (function(){

   var $ = this;

   return function() {

       // $ -> jQuery

   };

}).call(jQuery);
0
source

Standard in my workgroup:

jQuery.noConflict();
(function ($) {
    //Do jQuery stuff using $ here.
})(jQuery);
//Do prototype stuff using $ here
0
source

Source: https://habr.com/ru/post/1768924/


All Articles