Javascript:检测窗口何时完全加载 [英] Javascript: Detect when a window is fully loaded
问题描述
我有一个脚本,可以在网页生命周期的任何阶段加载该脚本. 加载脚本后,必须运行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屋!