Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

document ready javascript vanilla

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

How to create $(document).ready() for vanilla JavaScript

// 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>
Comment

document ready vanilla js

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

vanilla document.ready function

//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);
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery on body click 
Javascript :: js date add 1 day 
Javascript :: toastr.js cdn 
Javascript :: javascript audio stop 
Javascript :: remove and add active class with jquery 
Javascript :: document on click jquery 
Javascript :: execute javascript on page load jquery 
Javascript :: javascript float element right 
Javascript :: create new react app 
Javascript :: blacklisted word discord.js 
Javascript :: modal.show jquery 
Javascript :: select random element js array 
Javascript :: check if a date time string is a valid date in js 
Javascript :: Appium click on element Javascript 
Javascript :: pass url params to child router express 
Javascript :: npx for windows 
Javascript :: react empty space 
Javascript :: how to sort string aray in ts 
Javascript :: upgrading node on mac 
Javascript :: javascript find file extension from string 
Javascript :: react doesnt load local images but external does 
Javascript :: jquery scroll to specific div 
Javascript :: react native seperator code 
Javascript :: run react native app in production mode 
Javascript :: convert english number to bangla in javascript 
Javascript :: fontawsome icon size react element 
Javascript :: js get first object value 
Javascript :: jquery if checkbox checked 
Javascript :: install heroicons 
Javascript :: clear async storage react native 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =