views:

1819

answers:

4

I know I can use different frameworks like prototype or jquery to attach a function to the window.onload, but's not what I'm looking for.

I need something like .readyState so that I can do something like this:

if(document.isReady){
  var id = document.getElem ...
}

is there any other way than using what the frameworks do?

+1  A: 

jQuery doesn't use window.onload...

$(document).ready() waits until the DOM has loaded and can be traversed (the rest of the content may or may not be loaded by that point).

If you pull up the source for jQuery and sort through the mess...you'll find the work is done by the bindReady() method which has several different implementations for different browsers and only when all of those implementations fail does it fall back on listening for the load event for the window.

Justin Niessner
I'll have a look.
kristian nissen
+1  A: 

This is actually one of the biggest reasons most people use frameworks like jQuery because the solution to this is not consistent across browsers.

Scott Fox
I know, and I am a heavy user of jquery myself, but in this case I do not want to rely on a framework, I have been asked to avoid it.
kristian nissen
I would suggest possibly looking at the source to jQuery and porting *just* the code you want into your project.
Scott Fox
+2  A: 

While I usually advocate avoiding using frameworks unless necessary, I'd say using one in this case is perfectly fine. Here's jQuery:

$(function () {
    // do stuff after DOM has loaded
});

Note that it is NOT the same as an window.onload event, since onload executes first after other resources have been loaded (images etc.) The code I used in my example will execute when the DOM has finished loading, i.e., when the full HTML structure is available (not necessarily when images, CSS, etc. is available.)

If you want a checkable variable, you can set one in the ready-function:

var documentIsReady = false;
$(function () { documentIsReady = true; });

Of course you can find even more light-weight libraries than jQuery if all you want to do is to check for DOM-ready. But use a library in cases where different browsers behave very differently (this is one such case.)

Using some code from the DOMAssistant library, making your own "DOM is ready" function shouldn't be too hard:

var domLoaded = function (callback) {
    /* Internet Explorer */
    /*@cc_on
    @if (@_win32 || @_win64)
        document.write('<script id="ieScriptLoad" defer src="//:"><\/script>');
        document.getElementById('ieScriptLoad').onreadystatechange = function() {
            if (this.readyState == 'complete') {
                callback();
            }
        };
    @end @*/
    /* Mozilla, Chrome, Opera */
    if (document.addEventListener) {
        document.addEventListener('DOMContentLoaded', callback, false);
    }
    /* Safari, iCab, Konqueror */
    if (/KHTML|WebKit|iCab/i.test(navigator.userAgent)) {
        var DOMLoadTimer = setInterval(function () {
            if (/loaded|complete/i.test(document.readyState)) {
                callback();
                clearInterval(DOMLoadTimer);
            }
        }, 10);
    }
    /* Other web browsers */
    window.onload = callback;
};

Not tested, but it should work. I simplified it from DOMAssistant, because DOMAssistant allows multiple callbacks and has checking to make sure you can't add the same function twice etc.

Blixt
It's the only thing I need from a framework, everything else it has to serve, I don't need, so if possible, I would prefer not to use a framework in this case, otherwise I would go with jquery
kristian nissen
Okay, I added some simplified code from DOMAssistant, you could try that. According to their site it should work in all the major browsers. http://www.domassistant.com/documentation/DOMAssistantLoad-module.php
Blixt
My code will call `callback` up to two times (since `window.onload` should execute in all browsers.) When handling the event, check if `callback` has already been called and return if it has (this is what DOMAssistant does but I removed that part.)
Blixt
A: 

I use this:

if (document && document.getElementById
             && document.getElementsByTagName
             && document.body
             && document.head)
{
  [code]
}

Without the line breaks of course. I added those so it will be easy on the eyes.

You can add in other DOM methods you use, or not. Those are the primary ones; if they're not loaded, most likely the other methods aren't.

What happened here? As far as I've seen, most document methods are treated as functions. Functions not loaded, functions don't exist. If they don't exist, statement returns false, code not ran.

Eric
This seems to be apparently not working
Mithun P
I thought so too, until I did this:Go to any page, and while it's loading stop it (Esc button or whatever). Type the code in a console, replace [code] with alert('Hi') or something like that. Then add an else, with a code of alert('Bye'). You'll see it working.
Eric

related questions