Created
October 18, 2018 05:41
-
-
Save shiningjason/a22e7939cce0d6811242efaa1245722e to your computer and use it in GitHub Desktop.
document.ready in vanilla JS
This file contains hidden or 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
| (function initDocumentReadyListener() { | |
| var ie = !!(window.attachEvent && !window.opera); | |
| var wk = /webkit\/(\d+)/i.test(navigator.userAgent) && (RegExp.$1 < 525); | |
| var fn = []; | |
| var run = function () { for (var i = 0; i < fn.length; i++) fn[i](); }; | |
| var d = document; | |
| d.ready = function (f) { | |
| if (!ie && !wk && d.addEventListener) return d.addEventListener('DOMContentLoaded', f, false); | |
| if (fn.push(f) > 1) return; | |
| if (ie) { | |
| (function () { | |
| try { d.documentElement.doScroll('left'); run(); } | |
| catch (err) { setTimeout(arguments.callee, 0); } | |
| })(); | |
| } else if (wk) { | |
| var t = setInterval(function () { | |
| if (/^(loaded|complete)$/.test(d.readyState)) clearInterval(t), run(); | |
| }, 0); | |
| } | |
| }; | |
| })(); | |
| document.ready(function() { | |
| // do something | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment