سؤال

Super easy one. Using something like Mootools or JQuery or just plain old JS. Can you select a node to work on before the window being ready

For instance (sry for my short hand)

<script>
my = $('main');
my.danceLevel = 1000000;

WindowReady(){
dance(my);
}

</script>

Since the window is not ready can you count on elements being substantiated? Is window ready it being ready to render or being parsed etc..?

هل كانت مفيدة؟

المحلول

Assuming that "main" is an object in the body, the answer is no. "my = $('main');" will return undefined.

نصائح أخرى

No, elements will not be there so you can not reference them in in the head or before the element has been rendered.

Need to do it with jQuery document ready or place the script after the elements have been rendered.

You can do this, but you really shouldn't...

var yourElement;
function findElement() {
    var element = $("ElementID");
    if (element) {
        clearInterval(int);
        yourElement = element;
        //Do code here.
    }
}
var int = setInterval("findElement()", 10);

Why can't you just rely on the DOM Ready function of your JS Library of choice? Unless the page markup is GIGANTIC, the time it takes for the DOM to be ready vs the speed gain from this method will be minimal at best. This will just add unnecessary complexity to your scripts.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top