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 :: how to creat a function 
Javascript :: javascript how to change anchor style 
Javascript :: javascript add spaces to string 
Javascript :: joi enum validation 
Javascript :: javascript print all items in array 
Javascript :: dropzone add download button 
Javascript :: javascript redirect 
Javascript :: url redirect javascript 
Javascript :: redirect user to another page javascript 
Javascript :: display none javascript 
Javascript :: change form action js 
Javascript :: js sort by property 
Javascript :: node redirection 
Javascript :: npm json server 
Javascript :: clear canvas 
Javascript :: dont trigger useeffect on start 
Javascript :: get file name nodejs 
Javascript :: random array javascript 
Javascript :: javascript remove first character from string 
Javascript :: javascript truncate string full word 
Javascript :: html add new line in js alert 
Javascript :: set a previous year to the current date in javascript 
Javascript :: avascript sleep 1 second 
Javascript :: write json file nodejs 
Javascript :: modify margin top javascript 
Javascript :: slice string javascript from index to space 
Javascript :: copy to clipboard javascript 
Javascript :: make image circle css react 
Javascript :: browser javascript update url without changing history 
Javascript :: double question mark javascript 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =