开发者

JS Window ready and script timings

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 bei开发者_运维知识库ng 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜