Bind handlers to DOM ready before jQuery is loaded.

http://benalman.com/

by Ben Alman

HTML

<!-- Idea from https://gist.github.com/663043 -->
<script>

// Create a "fake" jQuery function that accepts function arguments to be
// queued as DOM ready callbacks.

(function(window){
  
  var fake = window.jQuery = window.$ = function( fn ) {
    if ( Object.prototype.toString.call( fn ) === '[object Function]' ) {
      fake.queue.push( fn );
    }
  };
  
  fake.queue = [];
  
})(window);

$(function(){
  console.log(1);
});

$(function(){
  console.log(2);
});

</script>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>

<script>

$(function(){ // don't do this yet!
  console.log(3);
});

// Copy all previously fake-queued DOM ready callbacks into jQuery's
// internal queue.

(function( window, $ ){
  $.map( window.$.queue, $ );
  window.$ = $;
})( window, jQuery.noConflict() );

$(function(){
  console.log(4);
});

// ON DOM READY, CONSOLE LOGS 3 1 2 4
// 
// (Obviously, you don't want to bind more events to DOM ready until
// you've executed the queue copy code, that why #3 fires first)
    
</script>