├── bower.json ├── package.json ├── README.md ├── LICENSE └── domReady.js /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "domReady", 3 | "version": "2.0.1", 4 | "main": "domReady.js", 5 | "license": "MIT" 6 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "domReady", 3 | "version": "2.0.1", 4 | "description": "An AMD loader plugin for detecting DOM ready", 5 | "categories": [ 6 | "Loader plugins" 7 | ], 8 | "main": "domReady.js", 9 | "github": "https://github.com/requirejs/domReady", 10 | "bugs": { 11 | "web": "https://github.com/requirejs/domReady/issues" 12 | }, 13 | "repositories": [ 14 | { 15 | "type": "git", 16 | "url": "https://github.com/requirejs/domReady.git" 17 | } 18 | ], 19 | "license": "MIT", 20 | "volo": { 21 | "url": "https://raw.github.com/requirejs/domReady/{version}/domReady.js" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # domReady 2 | 3 | An AMD loader plugin for detecting DOM ready. 4 | 5 | Known to work in RequireJS, but should work in other 6 | AMD loaders that support the same loader plugin API. 7 | 8 | ## Docs 9 | 10 | See the [RequireJS API "Page Load Event Support/DOM Ready" section](http://requirejs.org/docs/api.html#pageload). 11 | 12 | ## Latest release 13 | 14 | The latest release will be available from the "latest" tag. 15 | 16 | ## License 17 | 18 | MIT 19 | 20 | ## Code of Conduct 21 | 22 | [jQuery Foundation Code of Conduct](https://jquery.org/conduct/). 23 | 24 | 25 | ## Where are the tests? 26 | 27 | They are in the [requirejs](https://github.com/requirejs/requirejs) and 28 | [r.js](https://github.com/requirejs/r.js) repos. 29 | 30 | ## History 31 | 32 | This plugin was in the [requirejs repo](https://github.com/requirejs/requirejs) 33 | up until the requirejs 2.0 release. 34 | 35 | ## Contributing 36 | 37 | domReady follows the [same contribution model as requirejs](http://requirejs.org/docs/contributing.html) and is considered a sub-project of requirejs. 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright jQuery Foundation and other contributors, https://jquery.org/ 2 | 3 | This software consists of voluntary contributions made by many 4 | individuals. For exact contribution history, see the revision history 5 | available at https://github.com/requirejs/domReady 6 | 7 | The following license applies to all parts of this software except as 8 | documented below: 9 | 10 | ==== 11 | 12 | Permission is hereby granted, free of charge, to any person obtaining 13 | a copy of this software and associated documentation files (the 14 | "Software"), to deal in the Software without restriction, including 15 | without limitation the rights to use, copy, modify, merge, publish, 16 | distribute, sublicense, and/or sell copies of the Software, and to 17 | permit persons to whom the Software is furnished to do so, subject to 18 | the following conditions: 19 | 20 | The above copyright notice and this permission notice shall be 21 | included in all copies or substantial portions of the Software. 22 | 23 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 27 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 28 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 29 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 30 | 31 | ==== 32 | 33 | Copyright and related rights for sample code are waived via CC0. Sample 34 | code is defined as all source code displayed within the prose of the 35 | documentation. 36 | 37 | CC0: http://creativecommons.org/publicdomain/zero/1.0/ 38 | 39 | ==== 40 | 41 | Files located in the node_modules directory, and certain utilities used 42 | to build or test the software in the test and dist directories, are 43 | externally maintained libraries used by this software which have their own 44 | licenses; we recommend you read them, as their terms may differ from the 45 | terms above. 46 | -------------------------------------------------------------------------------- /domReady.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @license domReady 2.0.1 Copyright jQuery Foundation and other contributors. 3 | * Released under MIT license, http://github.com/requirejs/domReady/LICENSE 4 | */ 5 | /*jslint */ 6 | /*global require: false, define: false, requirejs: false, 7 | window: false, clearInterval: false, document: false, 8 | self: false, setInterval: false */ 9 | 10 | 11 | define(function () { 12 | 'use strict'; 13 | 14 | var isTop, testDiv, scrollIntervalId, 15 | isBrowser = typeof window !== "undefined" && window.document, 16 | isPageLoaded = !isBrowser, 17 | doc = isBrowser ? document : null, 18 | readyCalls = []; 19 | 20 | function runCallbacks(callbacks) { 21 | var i; 22 | for (i = 0; i < callbacks.length; i += 1) { 23 | callbacks[i](doc); 24 | } 25 | } 26 | 27 | function callReady() { 28 | var callbacks = readyCalls; 29 | 30 | if (isPageLoaded) { 31 | //Call the DOM ready callbacks 32 | if (callbacks.length) { 33 | readyCalls = []; 34 | runCallbacks(callbacks); 35 | } 36 | } 37 | } 38 | 39 | /** 40 | * Sets the page as loaded. 41 | */ 42 | function pageLoaded() { 43 | if (!isPageLoaded) { 44 | isPageLoaded = true; 45 | if (scrollIntervalId) { 46 | clearInterval(scrollIntervalId); 47 | } 48 | 49 | callReady(); 50 | } 51 | } 52 | 53 | if (isBrowser) { 54 | if (document.addEventListener) { 55 | //Standards. Hooray! Assumption here that if standards based, 56 | //it knows about DOMContentLoaded. 57 | document.addEventListener("DOMContentLoaded", pageLoaded, false); 58 | window.addEventListener("load", pageLoaded, false); 59 | } else if (window.attachEvent) { 60 | window.attachEvent("onload", pageLoaded); 61 | 62 | testDiv = document.createElement('div'); 63 | try { 64 | isTop = window.frameElement === null; 65 | } catch (e) {} 66 | 67 | //DOMContentLoaded approximation that uses a doScroll, as found by 68 | //Diego Perini: http://javascript.nwbox.com/IEContentLoaded/, 69 | //but modified by other contributors, including jdalton 70 | if (testDiv.doScroll && isTop && window.external) { 71 | scrollIntervalId = setInterval(function () { 72 | try { 73 | testDiv.doScroll(); 74 | pageLoaded(); 75 | } catch (e) {} 76 | }, 30); 77 | } 78 | } 79 | 80 | //Check if document already complete, and if so, just trigger page load 81 | //listeners. Latest webkit browsers also use "interactive", and 82 | //will fire the onDOMContentLoaded before "interactive" but not after 83 | //entering "interactive" or "complete". More details: 84 | //http://dev.w3.org/html5/spec/the-end.html#the-end 85 | //http://stackoverflow.com/questions/3665561/document-readystate-of-interactive-vs-ondomcontentloaded 86 | //Hmm, this is more complicated on further use, see "firing too early" 87 | //bug: https://github.com/requirejs/domReady/issues/1 88 | //so removing the || document.readyState === "interactive" test. 89 | //There is still a window.onload binding that should get fired if 90 | //DOMContentLoaded is missed. 91 | if (document.readyState === "complete") { 92 | pageLoaded(); 93 | } 94 | } 95 | 96 | /** START OF PUBLIC API **/ 97 | 98 | /** 99 | * Registers a callback for DOM ready. If DOM is already ready, the 100 | * callback is called immediately. 101 | * @param {Function} callback 102 | */ 103 | function domReady(callback) { 104 | if (isPageLoaded) { 105 | callback(doc); 106 | } else { 107 | readyCalls.push(callback); 108 | } 109 | return domReady; 110 | } 111 | 112 | domReady.version = '2.0.1'; 113 | 114 | /** 115 | * Loader Plugin API method 116 | */ 117 | domReady.load = function (name, req, onLoad, config) { 118 | if (config.isBuild) { 119 | onLoad(null); 120 | } else { 121 | domReady(onLoad); 122 | } 123 | }; 124 | 125 | /** END OF PUBLIC API **/ 126 | 127 | return domReady; 128 | }); 129 | --------------------------------------------------------------------------------