Using JQuery and Prototype in the same page

Currently you can do something like this: <head> <script type=”text/javascript” src=”https://stackoverflow.com/obp/js/prototype.js”></script> <script type=”text/javascript” src=”https://stackoverflow.com/obp/js/jquery.js”></script> <script type=”text/javascript”> var $j = jQuery.noConflict(); </script> </head> Then, use jQuery as $j() and Prototype’s $().

Restoring console.log()

Since original console is in window.console object, try restoring window.console from iframe: var i = document.createElement(‘iframe’); i.style.display = ‘none’; document.body.appendChild(i); window.console = i.contentWindow.console; // with Chrome 60+ don’t remove the child node // i.parentNode.removeChild(i); Works for me on Chrome 14.

What are the drawbacks of using synchronous ajax call?

Let’s remind you that JavaScript is single-threaded A synchronous IO call BLOCKS THE ENTIRE THREAD A simple fix is to use asynchronous style programming using callbacks. Customer = Class.create({ initialize: function(customerId, cb) { new Ajax.Request(‘some-url’, { method: ‘get’, parameters: { customerId: customerId }, onSuccess: (function() { this.setCustomerInfo.apply(this, arguments); cb.apply(this, arguments); }).bind(this) } }, setCustomerInfo: function(response) … Read more

How to pass Ruby variables to a JavaScript function in a Rails view?

A few options: escape_javascript Alias: j. Works only on strings. Escapes characters that can have special meanings in Javascript strings, like backslash escapes, into a format suitable to put inside Javascript string literal quotes. Maintains html_safe status of input, so needs html_safe otherwise special HTML chars like < would get escaped into &lt;. <% a … Read more

Trigger an event with Prototype

event.simulate.js fits your needs. I’ve used this several times and it works like a charm. It allows you to manually trigger native events, such as click or hover like so: $(‘foo’).simulate(‘click’); The great thing about this is that all attached event handlers will still be executed, just as if you would have clicked the element … Read more

JSON.stringify() array bizarreness with Prototype.js

Since JSON.stringify has been shipping with some browsers lately, I would suggest using it instead of Prototype’s toJSON. You would then check for window.JSON && window.JSON.stringify and only include the json.org library otherwise (via document.createElement(‘script’)…). To resolve the incompatibilities, use: if(window.Prototype) { delete Object.prototype.toJSON; delete Array.prototype.toJSON; delete Hash.prototype.toJSON; delete String.prototype.toJSON; }