Javascript:检测窗口何时完全加载 [英] Javascript: Detect when a window is fully loaded

查看:58
本文介绍了Javascript:检测窗口何时完全加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,可以在网页生命周期的任何阶段加载该脚本. 加载脚本后,必须运行initialize()方法.

I have a script which may be loaded at any stage of the life cycle of a web page. When the script is loaded it must run an initialize() method.

我希望此函数在"onload"事件上运行,但不能确定页面尚未加载,即"onload"尚未触发.

I want this function to be run on the "onload" event, but I cannot be certain that the page has not already loaded, i.e. that the "onload" has not been fired already.

理想情况下,我的脚本如下所示:

Ideally my script would look like this:

var _initialize = function() { ...};

if(window.LOADED)
    _initialize();
else if (window.addEventListener)
    window.addEventListener('load', _initialize, false); 
else if (window.attachEvent)
    window.attachEvent('onload', _initialize);

是否有这样的window.LOADED或document.LOADED变量或类似的东西?

Is there any such window.LOADED or document.LOADED variable or something similar?

预先感谢, 肖恩

推荐答案

您可以使用jQuery来做到这一点. $(document).ready().检查

You can use jQuery to do this. $(document).ready(). Check this

只需在ready中调用所需的函数即可.文档完全加载后,便会执行

Just call the function you want inside the ready. Then it'll be executed once the document is fully loaded

这篇关于Javascript:检测窗口何时完全加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆