Unit Testing with JUnit 4 and Spring | Home | But It Worked On Dev!

Filed under Quick Tips on May 16, 2008 by Corey Maass

A jQuery Tip: Don't Use jQuery

Sometimes, there's too much of a good thing. jQuery fires its "$(document).ready()" as soon as the DOM is loaded, but what if that's too soon?

I recently ran into a problem when I was using a script that will resize an iframe, but the iframe hadn't loaded yet. So, after some debugging I realized that--for once--jQuery was not the answer. Resorting to the classic window.onload, which waits until everything is loaded, solved the problem.

The real solution here? Sometimes a Ferrari just won't replace the old pick-up truck.

Post a Comment Digg Del.icio.us

Trackback Pings (TrackBack URL for this entry)

http://www.arc90.com/cgi-bin/mt4/mt-tb.cgi/146.

Comments

Corey, I think you're too quick to give jQuery the boot!

jQuery - by design - does not use window.onload for ready(), because then events can't be triggered until the whole window is loaded. ready() intentionally activates as soon as the DOM is accessible - this means that events can be attached sooner and things seem snappier in general.

There's a pretty good writeup about that here:
http://www.learningjquery.com/2006/09/introducing-document-ready

Luckily for us though, that does not preclude using jQuery to provide fuller functionality, even with window.onload. I haven't tested, but I'm pretty sure you can use the following:

$(window).load(function() { alert('The window is loaded!'); });

To still use jQuery-style syntax and functionality.

Posted on May 16, 2008 12:41 PM by Chris D

I recently looked back at code I wrote using YUI and Prototype, and I noticed that most of my code was still regular JavaScript. I think the problem with jQuery is that it tries to replace JavaScript altogether. I also looked at code I and other people wrote using jQuery, and I realized that there's very little JavaScript in there. Everything is wrapped, and that bothers me.

With YUI, you're still using JavaScript, but using YUI functions when needed, but with jQuery, you somehow end up using it for everything. I believe that it will end up being a crutch from which we will have trouble getting back.

Posted on May 25, 2008 1:12 PM by Alex M

Post a Comment:

Unit Testing with JUnit 4 and Spring | Main | But It Worked On Dev!