“Como criar $ (document) .ready () para JavaScript de baunilha” Respostas de código

JS baunilla dom pronta

document.addEventListener("DOMContentLoaded", function() {
  // code
});
Joyous Jaguar

Como criar $ (document) .ready () para JavaScript de baunilha

// You have 2 solution [1- simple that suport only new browsers]
document.addEventListener("DOMContentLoaded", function() {
  alert('dom is ready ^-^');
});
// [2- full solution that suport all browsers by using domready.js]
// step 1 - add to page head
<script>
(function(){var DomReady = window.DomReady = {};var userAgent = navigator.userAgent.toLowerCase();var browser = {version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1],safari: /webkit/.test(userAgent),opera: /opera/.test(userAgent),msie: (/msie/.test(userAgent)) && (!/opera/.test( userAgent )),mozilla: (/mozilla/.test(userAgent)) && (!/(compatible|webkit)/.test(userAgent))};var readyBound = false;var isReady = false;var readyList = [];function domReady() {if(!isReady) {isReady = true;if(readyList) {for(var fn = 0; fn < readyList.length; fn++) {readyList[fn].call(window, []);}readyList = [];}}};function addLoadEvent(func) {  var oldonload = window.onload;  if (typeof window.onload != 'function') {window.onload = func;  } else {window.onload = function() {  if (oldonload) {oldonload();  }  func();}  }};function bindReady() {if(readyBound) {return;}readyBound = true;if (document.addEventListener && !browser.opera) {document.addEventListener("DOMContentLoaded", domReady, false);}if (browser.msie && window == top) (function(){if (isReady) return;try {document.documentElement.doScroll("left");} catch(error) {setTimeout(arguments.callee, 0);return;}domReady();})();if(browser.opera) {document.addEventListener( "DOMContentLoaded", function () {if (isReady) return;for (var i = 0; i < document.styleSheets.length; i++)if (document.styleSheets[i].disabled) {setTimeout( arguments.callee, 0 );return;}domReady();}, false);}if(browser.safari) {var numStyles;(function(){if (isReady) return;if (document.readyState != "loaded" && document.readyState != "complete") {setTimeout( arguments.callee, 0 );return;}if (numStyles === undefined) {var links = document.getElementsByTagName("link");for (var i=0; i < links.length; i++) {if(links[i].getAttribute('rel') == 'stylesheet') {numStyles++;}}var styles = document.getElementsByTagName("style");numStyles += styles.length;}if (document.styleSheets.length != numStyles) {setTimeout( arguments.callee, 0 );return;}domReady();})();}addLoadEvent(domReady);};DomReady.ready = function(fn, args) {bindReady();if (isReady) {fn.call(window, []);} else {readyList.push( function() { return fn.call(window, []); } );}};bindReady();})();
</script>
// step 2 - use in your pages
<script>
DomReady.ready(function() {alert('dom is ready ^-^');});
</script>
sumer5020

Documento pronto para baunilha JS

if (document.readyState === "complete") { init(); }
Different Dogfish

Document.ready função de baunilha

//Document ready function (Vanilla
(function(funcName, baseObj) {
    // The public function name defaults to window.docReady
    // but you can pass in your own object and own function name and those will be used
    // if you want to put them in a different namespace
    funcName = funcName || "docReady";
    baseObj = baseObj || window;
    var readyList = [];
    var readyFired = false;
    var readyEventHandlersInstalled = false;

    // call this when the document is ready
    // this function protects itself against being called more than once
    function ready() {
        if (!readyFired) {
            // this must be set to true before we start calling callbacks
            readyFired = true;
            for (var i = 0; i < readyList.length; i++) {
                // if a callback here happens to add new ready handlers,
                // the docReady() function will see that it already fired
                // and will schedule the callback to run right after
                // this event loop finishes so all handlers will still execute
                // in order and no new ones will be added to the readyList
                // while we are processing the list
                readyList[i].fn.call(window, readyList[i].ctx);
            }
            // allow any closures held by these functions to free
            readyList = [];
        }
    }

    function readyStateChange() {
        if ( document.readyState === "complete" ) {
            ready();
        }
    }

    // This is the one public interface
    // docReady(fn, context);
    // the context argument is optional - if present, it will be passed
    // as an argument to the callback
    baseObj[funcName] = function(callback, context) {
        if (typeof callback !== "function") {
            throw new TypeError("callback for docReady(fn) must be a function");
        }
        // if ready has already fired, then just schedule the callback
        // to fire asynchronously, but right away
        if (readyFired) {
            setTimeout(function() {callback(context);}, 1);
            return;
        } else {
            // add the function and context to the list
            readyList.push({fn: callback, ctx: context});
        }
        // if document already ready to go, schedule the ready function to run
        if (document.readyState === "complete") {
            setTimeout(ready, 1);
        } else if (!readyEventHandlersInstalled) {
            // otherwise if we don't have event handlers installed, install them
            if (document.addEventListener) {
                // first choice is DOMContentLoaded event
                document.addEventListener("DOMContentLoaded", ready, false);
                // backup is window load event
                window.addEventListener("load", ready, false);
            } else {
                // must be IE
                document.attachEvent("onreadystatechange", readyStateChange);
                window.attachEvent("onload", ready);
            }
            readyEventHandlersInstalled = true;
        }
    }
})("docReady", window);
Sore Spider

Respostas semelhantes a “Como criar $ (document) .ready () para JavaScript de baunilha”

Perguntas semelhantes a “Como criar $ (document) .ready () para JavaScript de baunilha”

Mais respostas relacionadas para “Como criar $ (document) .ready () para JavaScript de baunilha” em JavaScript

Procure respostas de código populares por idioma

Procurar outros idiomas de código