Home  >  Article  >  Web Front-end  >  DOM ready application skills in javascript IE_javascript skills

DOM ready application skills in javascript IE_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:02:29816browse

If you only need to operate on the DOM, then there is no need to wait until the page is fully loaded. We need a faster way.
Firefox has a DOMContentLoaded event that can be easily solved, but unfortunately IE does not.
MSDN has an inconspicuous saying about a method of JSCRIPT. When the page DOM has not been loaded, an exception will be generated when the doScroll method is called. Then we use it in reverse. If there is no exception, then the page DOM has been loaded!

Copy code The code is as follows:

function IEContentLoaded (w, fn) { d = w.document, done = false,
// only fire once
init = function () {
if (!done) {           done = true;
                                                                                                     
            // throws errors until after ondocumentready                                                                                               'left');
} catch (e) {
setTimeout(arguments.callee, 50);                                                                                                                                                                 ;                                     == 'complete') {                                                                                                     .onreadystatechange = null; Diego Perini released this method in 2007,
And it has been widely recognized, so much so that many open source frameworks now draw on this method, such as ready in JQuery.

If you need to use IE’s DomReady in the future, it’s him.

Usage:
IEContentLoaded( document.getElementById("test") , test );

function test(){ }
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn