javascript domready中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript domready中相关的知识,希望对你有一定的参考价值。

/*!
 * domready (c) Dustin Diaz 2012 - License MIT
 */
! function(name, definition) {
    if (typeof module != 'undefined') module.exports = definition()
    else if (typeof define == 'function' && typeof define.amd == 'object') define(definition)
    else this[name] = definition()
}('domready', function(ready) {

    var fns = [],
        fn, f = false,
        doc = document,
        testEl = doc.documentElement,
        hack = testEl.doScroll,
        domContentLoaded = 'DOMContentLoaded',
        addEventListener = 'addEventListener',
        onreadystatechange = 'onreadystatechange',
        readyState = 'readyState',
        loadedRgx = hack ? /^loaded|^c/ : /^loaded|c/,
        loaded = loadedRgx.test(doc[readyState])

        function flush(f) {
            loaded = 1
            while (f = fns.shift()) f()
        }

    doc[addEventListener] && doc[addEventListener](domContentLoaded, fn = function() {
        doc.removeEventListener(domContentLoaded, fn, f)
        flush()
    }, f)


    hack && doc.attachEvent(onreadystatechange, fn = function() {
        if (/^c/.test(doc[readyState])) {
            doc.detachEvent(onreadystatechange, fn)
            flush()
        }
    });
    return (ready = hack ?
        function(fn) {
            self != top ?
                loaded ? fn() : fns.push(fn) :
                function() {
                    try {
                        testEl.doScroll('left')
                    } catch (e) {
                        return setTimeout(function() {
                            ready(fn)
                        }, 50)
                    }
                    fn()
            }()
        } :
        function(fn) {
            loaded ? fn() : fns.push(fn)
        })
})

以上是关于javascript domready中的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript domready.js

mass的domReady方案

domReady的兼容性实现方法

解决vue解析出现闪烁

DOMReady

domReady