Created
September 21, 2016 16:41
-
-
Save foo123/fd5531ce8279fe864feb82ccd062e5ba to your computer and use it in GitHub Desktop.
modified jquery.execReady.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
smoothState-wordpress integration script https://gist.github.com/foo123/c404aeeebbd0a26aceba122ac915aee1