检测页面是否已完成加载

Cam*_*ron 86 jquery

有没有办法检测页面何时完成加载,即所有内容,javascript和资产,如CSS和图像?

所以喜欢:

if(PAGE HAS FINISHED LOADING)
{
// do something amazing
}
Run Code Online (Sandbox Code Playgroud)

此外,如果页面加载时间超过1分钟,则执行其他操作,例如:

if(PAGE HAS BEEN LOADING FOR 1 MIN)
{
// do something else amazing
}
Run Code Online (Sandbox Code Playgroud)

我已经看到像Apple的MobileMe这样的网站做了类似的检查但是却无法在他们庞大的代码库中找到它.

有人可以帮忙吗?

谢谢

编辑:这基本上是我想要做的:

// hide content
$("#hide").hide();

// hide loading
$("#loading").hide();

// fade in loading animation
setTimeout($('#loading').fadeIn(), 200);

jQuery(window).load(function() {
  $("#hide").fadeIn();

  $("#loading").fadeOut(function() {
    $(this).remove();
    clearInterval(loadingAnim);
  });

  setTimeout(function() {
    $("#error").fadeIn();
  }, 60000);
});
Run Code Online (Sandbox Code Playgroud)

TJ.*_*TJ. 116

jQuery(window).load(function () {
    alert('page is loaded');

    setTimeout(function () {
        alert('page is loaded and 1 minute has passed');   
    }, 60000);

});
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/tangibleJ/fLLrs/1/

有关jQuery(窗口).load的说明,另请参见http://api.jquery.com/load-event/.

更新

有关javascript加载如何工作以及两个事件DOMContentLoadedOnLoad的详细说明可以在此页面上找到.

DOMContentLoaded:当DOM准备好被操作时.jQuery捕获此事件的方法是jQuery(document).ready(function () {});.

OnLoad:当DOM准备就绪并且所有资产 - 包括图像,iframe,字体等 - 已经加载并且旋转轮/小时类消失.jQuery捕获此事件的方法如上所述jQuery(window).load.

  • 请注意,`jQuery.load()` 自 jQuery/1.8 起已被弃用,并在 jQuery/3 中被移除。当前语法是`jQuery(window).on("load", function(){/*...*/});`。 (5认同)

sam*_*one 56

根据您的需求,有两种方法可以在jquery中执行此操作.

使用jquery你可以做到

  • 这可以通过setTimeout(function(){!loaded && foo();},20000)完成; 并在doc ready或window load中设置加载为true (2认同)
  • 对于 jquery 3.0 使用 $(window).on("load", function (e) { }); (2认同)

Nem*_*nja 12

这叫做onload.DOM ready实际上是出于onload等待图像的确切原因而创建的.(不久前在一个类似的问题上从Matchu那里得到的答案.)

window.onload = function () { alert("It's loaded!") }
Run Code Online (Sandbox Code Playgroud)

onload等待作为文档一部分的所有资源.

链接到他解释所有问题的问题:

点击我,你知道你想要的!


qwe*_*ymk 11

我认为最简单的方法是

var items = $('img, style, ...'), itemslen = items.length;

items.bind('load', function(){ 
    itemslen--;
    if (!itemlen) // Do stuff here
});
Run Code Online (Sandbox Code Playgroud)

编辑,有点疯狂:

var items = $('a, abbr, acronym, address, applet, area, audio, b, base, ' + 
    'basefont, bdo, bgsound, big, body, blockquote, br, button, canvas, ' + 
    'caption, center, cite, code, col, colgroup, comment, custom, dd, del, ' +
    'dfn, dir, div, dl, document, dt, em, embed, fieldset, font, form, frame, ' +
    'frameset, head, hn, hr, html, i, iframe, img, input, ins, isindex, kbd, ' +
    'label, legend, li, link, listing, map, marquee, media, menu, meta, ' +
    'nextid, nobr, noframes, noscript, object, ol, optgroup, option, p, ' +
    'param, plaintext, pre, q, rt, ruby, s, samp, script, select, small, ' + 
    'source, span, strike, strong, style, sub, sup, table, tbody, td, ' + 
    'textarea, tfoot, th, thead, title, tr, tt, u, ul, var, wbr, video, ' + 
    'window, xmp'), itemslen = items.length;

items.bind('load', function(){ 
    itemslen--;
    if (!itemlen) // Do stuff here
});
Run Code Online (Sandbox Code Playgroud)


Roh*_*mar 7

您可以检查document.readyState的另一个选项,

var chkReadyState = setInterval(function() {
    if (document.readyState == "complete") {
        // clear the interval
        clearInterval(chkReadyState);
        // finally your page is loaded.
    }
}, 100);
Run Code Online (Sandbox Code Playgroud)

从readyState页面的文档中可以看出完整状态的摘要

在文档加载时返回"加载",在完成解析但仍加载子资源时返回"交互",并在加载后"完成".


Cam*_*ron 6

供评论中询问的人参考,这是我在项目中实际使用的:

function onLoad(loading, loaded) {
    if(document.readyState === 'complete'){
        return loaded();
    }
    loading();
    if (window.addEventListener) {
        window.addEventListener('load', loaded, false);
    }
    else if (window.attachEvent) {
        window.attachEvent('onload', loaded);
    }
};

onLoad(function(){
   console.log('I am waiting for the page to be loaded');
},
function(){
    console.log('The page is loaded');
});
Run Code Online (Sandbox Code Playgroud)