views:

285

answers:

5

What is the non-jquery equivalent of $(document).ready()?

+3  A: 

I believe you want window.onload

taspeotis
Bear in mind that most libraries do a lot more work behind the scenes for you than just wrapping "window.onload" to ensure the window content is *really* loaded -- if you are interested in handling this consistently across different browsers for something non-trivial, you'd be wise to look into a library implementation.
bmoeskau
window.onload is not the same as $(document).ready(). The latter is an event for when the DOM is *ready for manipulation*, not when everything has loaded. (http://docs.jquery.com/Tutorials:Introducing_$%28document%29.ready%28%29). It is actually a very complicated process to determine this for every browser (some have a domready event, some don't). Please don't use window.onload, it fires when everything in your page has loaded, which is often far after the page is displayed to the user.
Ryan Doherty
A: 

I don't think javascript has that function inbuilt. It is jQuery specific.

Checksum
You're right, but that's probably why he's asking for the non-jQuery *equivalent*.
David Thomas
@ricebowl, the OP's original question asked nothing to that effect. It simply said "What is $(document).ready() in javascript". That's why many of the answers seem to make no sense now.
Josh Einstein
Then -having looked at the question's revision history- I offer you a +1 by way of an apology (I didn't, by the way, down-vote your answer; I'm not a fan of the whole down-voting option, I prefer to leave comments like the above).
David Thomas
+3  A: 

The original question was "What is $(document).ready() in javascript?"

http://api.jquery.com/ready/

Josh Einstein
+2  A: 

In plain vanilla JavaScript, with no libraries? It's an error. $ is simply an identifier, and is undefined unless you define it.

jQuery defines $ as it's own "everything object" (also known as jQuery so you can use it without conflicting with other libraries). If you're not using jQuery (or some other library that defines it), then $ will not be defined.

Or are you asking what the equivalent is in plain JavaScript? In that case, you probably want window.onload, which isn't exactly equivalent, but is the quickest and easiest way to get close to the same effect in vanilla JavaScript.

Brian Campbell
+15  A: 

The nice thing about $(document).ready() is that it fires before window.onload. The load function waits until everything is loaded, including external assets and images. $(document).ready, however, fires when the DOM tree is complete and can be manipulated. If you want to acheive DOM ready, without jQuery, you might check into this library. Someone extracted just the ready part from jQuery. Its nice and small and you might find it useful:

domready at Google Code

Doug Neiner
Nice library @Doug, the only thing I don't like about it, is that uses *browser sniffing* heavily, seems that it's based on jQuery 1.2.x. Newer versions of jQuery (1.3+) don't do browser sniffing anymore, I like the idea, I might start something at github :-)...
CMS
@CMS- I'd be interested in contributing to such a project
Russ Cam
@CMS @Russ Cam ditto. I am `dcneiner` on github and would be happy to contribute if needed.
Doug Neiner
@Russ Cam, @Doug: excellent, my username at github is `cms`.
CMS
@CMS great, just followed you!
Doug Neiner