[英]Checking If the Browser Has Finished Loading the Page
I spent mad time the in the last couple of days trying to find some code that allows me to do something when the browser has finished loading the page.在过去的几天里,我花了很多时间试图找到一些代码,让我在浏览器完成页面加载时做一些事情。
I found this code at http://callmenick.com/post/check-if-everything-loaded-with-javascript我在http://callmenick.com/post/check-if-everything-loaded-with-javascript找到了这段代码
<script>
var everythingLoaded = setInterval(function() {
if (/loaded|complete/.test(document.readyState)) {
clearInterval(everythingLoaded);
$('div#loader').hide();
$('div#main-container').fadeIn(1200);
}
}, 10);
</script>
This piece of code does work, but for some reason that I can't understand it interferes with this function when the page is first loaded.这段代码确实有效,但由于某种原因,我无法理解它在首次加载页面时会干扰此功能。 But once I do any resize , the function fires但是一旦我做任何调整大小,函数就会触发
$(document).ready(function() {
equalTiles(); //sets the height equal to calculated width
$(window).on('load resize orientationchange',function(){
equalTiles();
});
});
jQuery's $(document).ready()
helper is different to the window load event. jQuery 的$(document).ready()
助手与窗口加载事件不同。 Here is the difference:这是区别:
$(document).ready(function() {
// The browser is now aware of the entire DOM structure,
// but images and other resources may not have finished downloading yet.
// Include code here that depends on a DOM element being present,
// but doesn't depend on images being fully loaded yet.
// It looks none of your code actually wants to be in here as it all depends on images.
});
$(window).on("load", function() {
// All images are loaded now.
// Include code here that depends on images being loaded.
equalTiles();
$('div#loader').hide();
$('div#main-container').fadeIn(1200);
$(window).on("resize orientationchange", function(){
equalTiles();
});
});
Alternatively to Josh's answer, and more commonly, pass in a function to the jQuery
or $
directly.替代 Josh 的答案,更常见的是,将函数直接传递给jQuery
或$
。
$(function() {
// code that runs once the window is ready
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.