jQuery's Passes Itself As An Argument To The "Ready" Event Callback

Posted September 24, 2009 at 3:02 PM by Ben Nadel

Tags: Javascript / DHTML

As I read through Cody Lindley's excellent jQuery Enlightenment book, I found myself dog-earing many pages that contained little tips and factoids that I had never seen before. One of these facts was that jQuery passes itself as an argument to the callback methods that you provide in the "ready" event bindings. While this might not seem at first like a useful piece of information, when you are working in an application that has "$" variable conflicts, having jQuery passed itself to your callbacks can make your life a whole lot easier.

To demonstrate this awesomeness, take a look at this code in which the "$" is already being used and must be released via the noConflict() method:

  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  • <html>
  • <head>
  • <title>jQuery Ready Event Argument</title>
  • <script type="text/javascript">
  •  
  • // Create a $ variable for conflict.
  • window.$ = "Conflicted$";
  •  
  • </script>
  • <script type="text/javascript" src="jquery-1.3.2.js"></script>
  • <script type="text/javascript">
  •  
  • // Call no-conflict to release jQuery "$" shorthand.
  • jQuery.noConflict();
  •  
  •  
  • // When the document is ready, output the current $ value
  • // to demonstrate that it is no longer jQuery.
  •  
  • jQuery(
  • function(){
  • // This is a check to see if the "$" variable
  • // exists if we are not using it as an argument.
  • document.write(
  • "Current ($): " + $ + "<br />"
  • );
  • }
  • );
  •  
  •  
  • // When you pass your anonymous, callback method to the
  • // document::ready event binding, jQUery passes the
  • // jQuery instance to the callback as an argument.
  •  
  • jQuery( document ).ready(
  • function( $ ){
  •  
  • // Output the jQuery version.
  • document.write( $.fn.jquery + "<br />" );
  •  
  • }
  • );
  •  
  •  
  • // This also works for the short-hand notation in which
  • // a callback method is passed directly to the jQuery
  • // object.
  •  
  • jQuery(
  • function( $ ){
  •  
  • // Output the jQuery version.
  • document.write( $.fn.jquery + "<br />" );
  •  
  • }
  • );
  •  
  • </script>
  • </head>
  • <body>
  • <!--- Body content here. --->
  • </body>
  • </html>

As you can see in the above code, the "$" variable is already being used; as such, jQuery must release its hold on it as the library's short-hand reference. However, because jQuery passes itself to the document::ready event callbacks, as long as we define the callback method parameter as "$", we can use the short-hand reference with impunity. And, in fact, when we run the above code, we get the following output:

Current ($): Conflicted$
1.3.2
1.3.2

Because the callback method parameters are closer in the variable lookup chain to the method body than the window scope, the method body will check the parameters (argument collection) and find the "$" reference before it ever gets to the window scope. This allows our entire callback method to operate under the assumption (fact) that the "$" variable now references the jQuery library.



Reader Comments

Sep 24, 2009 at 3:48 PM // reply »
34 Comments

Very cool, Ben. Thanks for sharing.


Sep 24, 2009 at 7:01 PM // reply »
11,241 Comments

@Marc,

Happy to share - I thought it was cool when I read it.


Sep 27, 2009 at 6:23 AM // reply »
14 Comments

@Ben

That is awesome! I run into that problem all of the time as a lot of our older applications run the Prototype library which uses $. I always end up replacing $ to "jQuery" using the noConflict method ... not now though :)

Good tip!


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 22, 2013 at 5:35 PM
Script Tags, jQuery, And Html(), Text() And Contents()
This is still an issue 2 years later. jQuery is supposed to remediate these cross browser issues, no? I have been unable to find any statement from the jQuery team calling this behavior "by de ... read »
May 22, 2013 at 12:44 PM
Ask Ben: Query Loop Inside CFScript Tags
In cf10, if you call a function that has: local.result = {}; local.result.msg = ""; local.svc = new query(); local.svc.setSQL("SELECT * FROM..."); local.obj = local.svc.exe ... read »
May 22, 2013 at 12:29 PM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben: What version of Java are you using? Also, did you test users.id to see what Java reports as the data type? I wonder if it's not a Java primitive data type, but getting returned as something ... read »
May 22, 2013 at 11:47 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Dana, Awesome - so it looks like this bug was fixed in ColdFusion 10. Thanks so much for double-checking that. ... read »
May 22, 2013 at 11:37 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
When I c&p and run on cf10, I get: Selected User IDs: 1,4 User 1 selected: YES - YES User 2 selected: NO - NO User 3 selected: NO - NO User 4 selected: YES - YES User 5 selected: NO - ... read »
May 22, 2013 at 11:27 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Tom, Good thought, but no dice. Both of these still exhibit the same behavior: users.id[ users.currentRow ] users[ "id" ][ users.currentRow ] It's just something whacky happening with ... read »
May 22, 2013 at 11:07 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
Could your problem be that "users.id" is actually an ARRAY, not a single value? Perhaps try it again with "users.id[1]" (I only have CF8 here at work). ... read »
May 22, 2013 at 7:52 AM
Nested Views, Routing, And Deep Linking With AngularJS
Hi, Just a quick thank you. As it happens, for my own purposes, the pending ui-router work being done in native angular is likely the one I'll adopt, but your exploration, code and documentation of ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools