Using jQuery with other Libraries
Tuesday, May 29, 2007
So you finally got the opportunity to try out jQuery on one of your projects but you are already using a library that commands control of the infamous $ alias or function. Fear not, for no matter the reason the $ may be taken, jQuery is built to be compact and versatile. All of jQuery is contained within the jQuery namespace and the $ is just an alias for jQuery. So the following two lines of code will result in the same output.
jQuery('.myClass').hide();
$('.myClass').hide();
With this knowledge creating a different alias is as simple as this line of code.
var $j = jQuery;
However, depending on the order the libraries have been loaded, jQuery might still be in control of the $ and causing existing scripts to fail. The easiest way to solve this would be to just change the order your scripts are included by making sure jQuery is loaded first. This allows the other library to overwrite the $ with its own implementation. Another way that is just as easy, is to use the noConflict method. The noConflict method returns the $ back to its original owner and allows you to create a new alias on the fly.
var $j = jQuery.noConflict();
Now you can use $j and take advantage of all the DOM-fu jQuery provides.
Posted in jQuery with 2 comments
Hi Brandon, I have some minor tips/fixes for your excellent bgiframe plugin…
The selector “iframe.bgiframe” does a deep search and can cause a slight delay when there are alot of child elements. Using
>iframe.bgiframeinstead helped alot.I found that the check for an existing bgiframe using
if ( !$('iframe.bgiframe', this)[0] )fails in some editions of IE6. Changing it toif ( $('>iframe.bgiframe',this).length == 0 )seems to fix it.Again for performance, I found it helpful to check for existing iframes at the start of the plugin rather than after building the html, so I changed the browser-test line to include a check for any items that do not already have a bgiframe:
if ( !($.browser.msie && typeof XMLHttpRequest == 'function') || this.not('[>iframe.bgiframe]') == 0 ) return this;(Tested with jQuery 1.1.1)
Kind regards, George
By George Adamson on Thursday, June 7, 2007 at 05:23 AM
Thanks George for the great ideas on increasing performance! I’ll for sure be implementing them soon.
By Brandon Aaron on Thursday, June 7, 2007 at 06:48 AM