$(document).ready 到底保证了什么?
在 Google 的 Chrome 浏览器中运行我的(相当复杂的)JavaScript/jQuery 应用程序,看起来 $(document).ready
会在某些 JavaScript 文件尚未加载时触发。
相关代码(简化):
在我的 HTML 文件中
<script>var httpRoot='../../../';var verifyLoad = {};</script>
<script src="../../../globalvars.js"></script>
<script src="../../../storagekeys.js"></script>
<script src="../../../geometry.js"></script>
<script src="../../../md5.js"></script>
<script src="../../../serialize.js"></script>
...
<script src="../../../main.js"></script>
作为除 main.js 之外的每个 .js 文件的最后一个语句:
verifyLoad.FOO = true; // where FOO is a property specific to the file
例如,
verifyLoad.jquerySupplements = true;
verifyLoad.serialize = true;
在 main.js 中:
$(document).ready(function() {
function verifyLoadTest (scriptFileName, token) {
if (!verifyLoad.hasOwnProperty(token)) {
console.log(scriptFileName + ' not properly loaded');
};
};
verifyLoadTest('globalvars.js', 'globalvars');
verifyLoadTest('storagekeys.js', 'storagekeys');
verifyLoadTest('geometry.js', 'geometry');
verifyLoadTest('md5.js', 'geometry');
verifyLoadTest('serialize.js', 'serialize');
...
}
令我惊讶的是,我看到了其中一些触发器。这与我对$(document).ready
的理解不符。我缺少什么?
Running my (rather complex) JavaScript/jQuery application in Google's Chrome browser, it would appear that $(document).ready
fires while some of the JavaScript files have not yet loaded.
The relevant code (simplified):
In my HTML file
<script>var httpRoot='../../../';var verifyLoad = {};</script>
<script src="../../../globalvars.js"></script>
<script src="../../../storagekeys.js"></script>
<script src="../../../geometry.js"></script>
<script src="../../../md5.js"></script>
<script src="../../../serialize.js"></script>
...
<script src="../../../main.js"></script>
As the last statement of each of the .js files except main.js:
verifyLoad.FOO = true; // where FOO is a property specific to the file
e.g.
verifyLoad.jquerySupplements = true;
verifyLoad.serialize = true;
In main.js:
$(document).ready(function() {
function verifyLoadTest (scriptFileName, token) {
if (!verifyLoad.hasOwnProperty(token)) {
console.log(scriptFileName + ' not properly loaded');
};
};
verifyLoadTest('globalvars.js', 'globalvars');
verifyLoadTest('storagekeys.js', 'storagekeys');
verifyLoadTest('geometry.js', 'geometry');
verifyLoadTest('md5.js', 'geometry');
verifyLoadTest('serialize.js', 'serialize');
...
}
Much to my amazement, I see some of these trigger. This does not match my understanding of $(document).ready
. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当浏览器从头到尾解析 HTML 文件并将其转换为 DOM 结构时,将触发文档的就绪事件。它不以任何方式保证任何其他资源(例如样式表、图像或本例中的脚本)将会加载。它仅引用 DOM 结构,并且无论页面资源的加载状态如何都会被触发。
如果您想等待资源加载,请使用
window
的load
事件,该事件仅在页面上的每个元素完成加载时触发。请参阅:
.load
.ready
The document's ready event is fired when the browser has parsed the HTML file from beginning to end and converted it into a DOM structure. It does not in any way guarantee that any other resources (e.g. stylesheets, images or, as in this case, scripts) will have loaded. It only refers to the DOM structure, and is fired irrespective of the loading status of the page's resources.
If you want to wait for resources to load, use the
window
'sload
event, which is fired only when every element on the page has finished loading.See:
.load
.ready