Things You Might Not Know about jQuery
answers.oreilly.com
answers.oreilly.com
The following two lines have the same effect, for example:
$('pre').attr("css", {backgroundColor:"gray"})
$('pre').css({backgroundColor:"gray"})
Not correct. The first line will create an attribute called 'css' while the second line will change the 'style' attribute. The author probably meant something like: $('pre').attr("style", {backgroundColor:"gray"})
$('pre').css({backgroundColor:"gray"})
...but even these two lines are not exactly the same. The first one will reset the style attribute to the given value while the second line will just change the backgroundColor value in the style attribute.Note to jeresig: this article illustrates a beef I remember passing on when the jQuery docs reorganized; it works well as API reference now, but before, introductory text for each section of the docs describing things like event namespacing made for a nicer introduction to beginners like me, as well as the structured sub-organization of methods within each section. Now, it's more of an alphabetical method soup, so a lot of beginners probably never bother to learn that event namespaces exist, or what the jQuery event object does. For example, you can only get to the documentation on the event object from within the documentation for methods like .bind(), which I find odd. (How would a beginner know that it even existed unless they went specifically to the .bind() docs in an alphabetical list of other cooler sounding methods?)
var image = $("").appendTo("body"); //Nothing appended to body.
I think it should be:
var image = $("<img>").appendTo("body"); //img node appended to body.
jQuery is a very powerful library, but some of its powerful features are obscure, and unless you've ready the jQuery source code, or my new book jQuery Pocket Reference, you may not know about them.
Why don't you give me the article first, let me read it, get amazed by the techniques and then find out about you and your book and feel like I need to read another book.
This way I feel like you are trying to sell me your stuff.
He is trying to sell you stuff, but honestly you should go right ahead and buy what he's advertising because his books are really that good.
Deleted comment
Are you under some illusion that sites run by for-profit companies have a primary purpose other than to sell you their stuff?
Maybe it's a little offputting. But I'd put dollars-to-donuts that if it was A/B tested, they would generate a lot more revenue with the notice at the top. (Edit - just noticed there was no purchase link, so it would need to be changed to easily A/B test...)
As far as I know, this was always the case. The reason people use $(document).ready() is that it feels more correct, is more consistent. jQuery() supports three distinct behaviors:
$(function) => jQuery(document).ready(function)
$(objects) => Wrap objects with jQuery functionality
$("selector") => Find objects, and wrap them with jQuery functionality
One of these is not like the others. $("<tag>") => Make a DOM element and wrap it
This wild overloading of $ is one of the things I find hard to deal with in jQuery. $(document).ready(handler);
you can write: $(handler); $(function() {
// write the stuff you normally put in $(document).ready( ... );
}); jQuery(function($) {
// your code using $
}(jQuery));
This way you can use $ to reference jQuery in your code even if the other library took control of $ in the global scope. jQuery(function($) {
// your code using $
});
You might be thinking of the recommended method of creating a closure, in which the code is executed immediately rather than upon page load: (function($) {
// your code using $
})(jQuery);You need the jQuery parameter to make sure the $ in your code refers to the jQuery object and not something else. Without it your code is just referencing the global $ object which some other framework could have poached for itself.
But I suspect you already know all that. What his code is actually doing is creating an anonymous function intended to provide a safe place for $-based code, evaluating it immediately, and then passing the return value to the jQuery function. Depending on the return value this may be useful or not, but I'm pretty sure it's not what he intended. His function is evaluated immediately rather than when the DOM is ready.
What you want to do is combine the second part of your example with your already-written code:
(function($) {
$(function () {
// your code to fire when the DOM is ready
});
})(jQuery);from the docs:
var j = jQuery.noConflict();
// Do something with jQuery
j("div p").hide();
// Do something with another library's $()
$("content").style.display = 'none';It reads as if the author is trying to say that the reason for using $(document).ready(handler) - to wait to run certain functions until the entire DOM has loaded - is no longer necessary.