Skip to content

Instantly share code, notes, and snippets.

@foo123
Created September 21, 2016 16:41
Show Gist options
  • Save foo123/fd5531ce8279fe864feb82ccd062e5ba to your computer and use it in GitHub Desktop.
Save foo123/fd5531ce8279fe864feb82ccd062e5ba to your computer and use it in GitHub Desktop.
modified jquery.execReady.js
/**
* Replace jQuery's $.fn.ready() function with a mod exec
*
* Sites that make heavy use of the $(document).ready function
* are generally incompatable with asynchrounous content. The
* the $.fn.ready function only runs once. This script replaces
* the ready function with a module execution controller that
* let's us register functions and execute all of the functions
* as we need them. This is useful after HTML gets injected on the
* page and we want to rebind functionally to the new content.
*
* @author Miguel Ángel Pérez [email protected]
* @note Should be placed directly after jQuery on the page
*
*/
// https://gist.github.com/miguel-perez/476046a42d229251fec3
;(function($){
"use strict";
window.addOriginalEventListener = window.addEventListener;
window.loadList = [];
window.addEventListener = function( event, handler, bubble ){
if ( "load" === event ) window.loadList.push( handler );
window.addOriginalEventListener( event, handler, bubble );
};
window.triggerLoad = function( clear ) {
if ( "function" === typeof window.onload )
{
var onload = window.onload;
window.onload = null;
try {
onload( );
}
catch (e) {
throw e;
}
}
for (var i=0; i<window.loadList.length; i++ )
{
try {
window.loadList[i]( );
}
catch (e) {
throw e;
}
}
if ( false !== clear ) window.loadList.length = 0;
};
document.addOriginalEventListener = document.addEventListener;
document.readyList = [];
document.addEventListener = function( event, handler, bubble ){
if ( "DOMContentLoaded" === event ) document.readyList.push( handler );
document.addOriginalEventListener( event, handler, bubble );
};
document.triggerReady = function( clear ){
for (var i=0; i<document.readyList.length; i++ )
{
try {
document.readyList[i]( );
}
catch (e) {
throw e;
}
}
if ( false !== clear ) document.readyList.length = 0;
};
/*document.addOriginalEventListener("DOMContentLoaded", function( ){
document.triggerReady( true );
}, false);
window.addOriginalEventListener("load", function( ){
window.triggerLoad( true );
}, false);*/
/** create mod exec controller */
$.readyFn = {
list: [],
register: function( fn ) {
$.readyFn.list.push(fn);
},
execute: function( el, clear ) {
el = el || document;
for (var i = 0; i < $.readyFn.list.length; i++)
{
try {
$.readyFn.list[i].call(el, $);
}
catch (e) {
throw e;
}
}
if ( false !== clear ) $.readyFn.list.length = 0;
},
clear: function( ) {
$.readyFn.list.length = 0;
}
};
/** register function */
$.fn.jqReady = $.fn.ready;
$.fn.ready = function(fn) {
$.readyFn.register(fn);
return this;
};
/** run all functions */
$(document).jqReady(function(){
$.readyFn.execute(this, true);
});
})(jQuery);
@foo123
Copy link
Author

foo123 commented Sep 21, 2016

smoothState-wordpress integration script https://gist.github.com/foo123/c404aeeebbd0a26aceba122ac915aee1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment