Combining Scriptaculous and JQuery in a Rails Application

I have the following situation.

  • A rails application that uses rjs / Scriptaculous to provide AJAX functionality
  • Lots of good javascript written using jQuery (for a standalone application)

I want to combine these two and use my jQuery-based functions in my Rails application, but I'm worried about conflicts between JQuery and Scriptaculous (they both define the $ () function, etc.).

What is my easiest option to combine the two? Thanks!

+4
source share
3 answers
jQuery.noConflict(); 

Then use jQuery instead of $ to reference jQuery. eg.

 jQuery('div.foo').doSomething() 

If you need to adapt jQuery code that uses $, you can surround it with the following:

 (function($) { ...your code here... })(jQuery); 
+15
source

I believe this is jQuery.noConflict() .

You can call it standalone as follows:

 jQuery.noConflict(); jQuery('div').hide(); 

Or you can assign it to another variable of your choice:

 var $j = jQuery.noConflict(); $j('div').hide(); 

Or you can continue to use the jQuery $ function inside the block as follows:

 jQuery.noConflict(); // Put all your code in your document ready area jQuery(document).ready(function($){ // Do jQuery stuff using $ $("div").hide(); }); // Use Prototype with $(...), etc. $('someid').hide(); 

For more information, see Using jQuery with Other Libraries in the jQuery Documentation.

+2
source

jRails is a replacement for a script / prototype in Rails using the jQuery library, it does exactly what you are looking for.

+1
source

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


All Articles