有没有办法检测一个页面何时加载完毕,即它的所有内容、javascript以及CSS和图像之类的assets资源 ?

so like:

if(PAGE HAS FINISHED LOADING)
{
// do something amazing
}

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

if(PAGE HAS BEEN LOADING FOR 1 MIN)
{
// do something else amazing
}

我看到像苹果的MobileMe这样的网站也做过类似的判断,但在他们庞大的代码库中还没有找到答案.

Can anyone help?

谢谢

EDIT: This is essentially what I want to do:

// 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);
});

推荐答案

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

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

});

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

See also http://api.jquery.com/load-event/ for an explanation on the jQuery(window).load.

Update

关于javascript加载工作原理以及DOMContentLoadedOnLoad两个事件的详细说明,请参见this page.

DOMContentLoaded:当DOM is ready被操纵时.jQuery捕获此事件的方式是使用jQuery(document).ready(function () {});.

OnLoad: When the DOM is ready and all assets - this includes images, iframe, fonts, etc - have been loaded and the spinning wheel / hour class disappear. jQuery's way of capturing this event is the above mentioned jQuery(window).load.

Jquery相关问答推荐

ASP.NET Core 8 MVC:从jQuery发布控制器中的所有值为空

如果链接包含多个关键字,jQuery 将 HTML 添加到链接

加载外部 css 文件,如 jquery 中的脚本,这在 ie 中也兼容

确定 Javascript/jQuery 中字符串的像素长度?

jQuery非只读 Select 器

Javascript 仅打印 iframe 内容

可以在不使用for=id的情况下将标签与复选框相关联吗?

JavaScript style.display="none" 还是 jQuery .hide() 更高效?

jQuery 序列化不注册复选框

jQuery淡出不显示无?

jQuery用另一个类替换一个类

单击后如何禁用提交按钮?

如何触发href元素的点击事件

通过 AJAX 和 jQuery 使用 HTML5 文件上传

为 jQuery AJAX 调用实现加载指示器

触发下拉更改事件

消除移动 Safari 中点击事件的 300 毫秒延迟

Underscore.js 和 jQuery 互补吗?

如何使用 jQuery 在悬停时显示工具提示消息?

如何获得两个日期对象之间的小时差?