Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 989131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:44:32+00:00 2026-05-16T05:44:32+00:00

I need to wait for document readyness in my JavaScript, to insert a div

  • 0

I need to wait for document readyness in my JavaScript, to insert a div at the bottom of the body.

I want to:

  • make this JavaScript file as small as possible (compile it down to < 1kb if possible)
  • inline the code that provides the document readyness in a closure (without exporting it)

Inlining the whole jQuery source in my file would be too big, so I’m looking for other methods. window.onload would work, but I specifically want document readyness, and not wait for the window.onload event.

Does anyone know a JS snippet that can do this? Or should I just copy part of jQuery’s source?

EDIT:

I managed to crawl the jQuery source and put together with the following snippet:

var ready = (function () {
    var ready_event_fired = false;
    var ready_event_listener = function (fn) {

        // Create an idempotent version of the 'fn' function
        var idempotent_fn = function () {
            if (ready_event_fired) {
                return;
            }
            ready_event_fired = true;
            return fn();
        }

        // The DOM ready check for Internet Explorer
        var do_scroll_check = function () {
            if (ready_event_fired) {
                return;
            }

            // If IE is used, use the trick by Diego Perini
            // http://javascript.nwbox.com/IEContentLoaded/
            try {
                document.documentElement.doScroll('left');
            } catch(e) {
                setTimeout(do_scroll_check, 1);
                return;
            }

            // Execute any waiting functions
            return idempotent_fn();
        }

        // If the browser ready event has already occured
        if (document.readyState === "complete") {
            return idempotent_fn()
        }

        // Mozilla, Opera and webkit nightlies currently support this event
        if (document.addEventListener) {

            // Use the handy event callback
            document.addEventListener("DOMContentLoaded", idempotent_fn, false);

            // A fallback to window.onload, that will always work
            window.addEventListener("load", idempotent_fn, false);

        // If IE event model is used
        } else if (document.attachEvent) {

            // ensure firing before onload; maybe late but safe also for iframes
            document.attachEvent("onreadystatechange", idempotent_fn);

            // A fallback to window.onload, that will always work
            window.attachEvent("onload", idempotent_fn);

            // If IE and not a frame: continually check to see if the document is ready
            var toplevel = false;

            try {
                toplevel = window.frameElement == null;
            } catch (e) {}

            if (document.documentElement.doScroll && toplevel) {
                return do_scroll_check();
            }
        }
    };
    return ready_event_listener;
})();

// TEST
var ready_1 = function () {
    alert("ready 1");
};
var ready_2 = function () {
    alert("ready 2");
};
ready(function () {
    ready_1();
    ready_2();
});

Thank you very much for helping me find this in the jQuery source. I can now put all this in a closure and do my work without exporting any functions and polluting the global scope.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:44:32+00:00Added an answer on May 16, 2026 at 5:44 am

    One option would be to just get the core.js jQuery file from github.

    You could probably slim it down quite a bit for code you don’t need. Then run it through YUI compressor, and it should be pretty small.

    • http://github.com/jquery/jquery/blob/1.4.2/src/core.js (jQuery core)
    • http://yui.2clics.net/ (YUI compressor online)

    I tried it, and this code worked properly:

    $(function() {
        var newDiv = document.createElement('div');
        document.getElementsByTagName('body')[0].appendChild(newDiv);
    });
    

    Update: This was as small as I got it. It is entirely from jQuery and is around 1,278 bytes (compressed). Should get smaller when you gzip.

    Only difference is that you need to call it like:

    $.fn.ready(function() {
        // your code
    });
    

    YUI Compressed:

    (function(){var e=function(i,j){},c=window.jQuery,h=window.$,d,g=false,f=[],b;e.fn={ready:function(i){e.bindReady();if(e.isReady){i.call(document,e)}else{if(f){f.push(i)}}return this}};e.isReady=false;e.ready=function(){if(!e.isReady){if(!document.body){return setTimeout(e.ready,13)}e.isReady=true;if(f){var k,j=0;while((k=f[j++])){k.call(document,e)}f=null}if(e.fn.triggerHandler){e(document).triggerHandler("ready")}}};e.bindReady=function(){if(g){return}g=true;if(document.readyState==="complete"){return e.ready()}if(document.addEventListener){document.addEventListener("DOMContentLoaded",b,false);window.addEventListener("load",e.ready,false)}else{if(document.attachEvent){document.attachEvent("onreadystatechange",b);window.attachEvent("onload",e.ready);var i=false;try{i=window.frameElement==null}catch(j){}if(document.documentElement.doScroll&&i){a()}}}};d=e(document);if(document.addEventListener){b=function(){document.removeEventListener("DOMContentLoaded",b,false);e.ready()}}else{if(document.attachEvent){b=function(){if(document.readyState==="complete"){document.detachEvent("onreadystatechange",b);e.ready()}}}}function a(){if(e.isReady){return}try{document.documentElement.doScroll("left")}catch(i){setTimeout(a,1);return}e.ready()}window.jQuery=window.$=e})();
    

    Full source (again, this is jQuery code):

    (function() {
    var jQuery = function( selector, context ) {
        },
        _jQuery = window.jQuery,
        _$ = window.$,
    
        rootjQuery,
        readyBound = false,
        readyList = [],
        DOMContentLoaded;
    
    jQuery.fn = {
        ready: function( fn ) {
            jQuery.bindReady();
            if ( jQuery.isReady ) {
                fn.call( document, jQuery );
            } else if ( readyList ) {
                readyList.push( fn );
            }
            return this;
        }
    };
    jQuery.isReady = false;
    jQuery.ready = function() {
            if ( !jQuery.isReady ) {
                if ( !document.body ) {
                    return setTimeout( jQuery.ready, 13 );
                }
                jQuery.isReady = true;
                if ( readyList ) {
                    var fn, i = 0;
                    while ( (fn = readyList[ i++ ]) ) {
                        fn.call( document, jQuery );
                    }
                    readyList = null;
                }
                if ( jQuery.fn.triggerHandler ) {
                    jQuery( document ).triggerHandler( "ready" );
                }
            }
        };
    jQuery.bindReady = function() {
            if ( readyBound ) {
                return;
            }
            readyBound = true;
    
            if ( document.readyState === "complete" ) {
                return jQuery.ready();
            }
            if ( document.addEventListener ) {
                document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
                window.addEventListener( "load", jQuery.ready, false );
            } else if ( document.attachEvent ) {
    
                document.attachEvent("onreadystatechange", DOMContentLoaded);
                window.attachEvent( "onload", jQuery.ready );
    
                var toplevel = false;
                try {
                    toplevel = window.frameElement == null;
                } catch(e) {}
                if ( document.documentElement.doScroll && toplevel ) {
                    doScrollCheck();
                }
            }
        };
    rootjQuery = jQuery(document);
    if ( document.addEventListener ) {
        DOMContentLoaded = function() {
            document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
            jQuery.ready();
        };
    } else if ( document.attachEvent ) {
        DOMContentLoaded = function() {
            if ( document.readyState === "complete" ) {
                document.detachEvent( "onreadystatechange", DOMContentLoaded );
                jQuery.ready();
            }
        };
    }
    function doScrollCheck() {
        if ( jQuery.isReady ) {
            return;
        }
        try {
    
            document.documentElement.doScroll("left");
        } catch(e) {
            setTimeout( doScrollCheck, 1 );
            return;
        }
        jQuery.ready();
    }
    window.jQuery = window.$ = jQuery;
    })();
    

    I’m sure there are more bytes that could be removed.

    Don’t forget:

    /*!
     * jQuery JavaScript Library v1.4.2
     * http://jquery.com/
     *
     * Copyright 2010, John Resig
     * Dual licensed under the MIT or GPL Version 2 licenses.
     * http://jquery.org/license
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to recalculate body's main div height, than wait while all content (images)
I need to change the mouse pointer to the wait cursor. I tried document.body.style.cursor
I am doing a media file scan using sendBroadcast. Then I need to wait
I need a way to wait until a (Swing) JComponent is fully painted. This
Need to apply a filter to a file like this: TUPAC_0006:1:1:2554:2356#0/1 0 * 0
should this work or do i need to wait for a new resharper release?
Like the title states, I need to make my program wait until my NSOpenPanel
I need files to be downloaded to /tmp/cron_test/. My wget code is wget --random-wait
I need to run two ffmpeg commands, one after the other i.e., wait until
I have a php script I need to run every 5 seconds (run, wait

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.