Javascript:检测窗口何时完全加载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6424064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: Detect when a window is fully loaded
提问by Shane
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.
我有一个可以在网页生命周期的任何阶段加载的脚本。加载脚本时,它必须运行 initialize() 方法。
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.
我希望这个函数在“onload”事件上运行,但我不能确定页面还没有加载,即“onload”还没有被触发。
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);
Is there any such window.LOADED or document.LOADED variable or something similar?
有没有这样的 window.LOADED 或 document.LOADED 变量或类似的东西?
Thanks in advance, Shane
提前致谢,谢恩
采纳答案by Balanivash
回答by Boopathi Rajaa
If you are not using any library you could make use of this
如果您没有使用任何库,您可以使用它
function somelib(){
this.readystate = false; //tracks the state of DOM
var nextcallfunc; //stores the function to be called after DOM is loaded
//called by checkReady function when DOM is loaded completely
this.ready = function(func){
// quit if this function has already been called
if (arguments.callee.done) return;
// flag this function so we don't do the same thing twice
arguments.callee.done = true;
this.readystate=true; //make DOM load permanent
nextcallfunc.call(); //calls the onload handler
}
//Checks if the DOM has loaded or not
this.checkReady= function(func){
/* for Mozilla/Opera9 */
nextcallfunc = func;
if (document.addEventListener) {
document.addEventListener("DOMContentLoaded", this.ready , false);
}
/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
if (this.readyState == "complete") {
ready(func); // call the onload handler
}
};
/*@end @*/
/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
var _timer = setInterval(function() {
if (/loaded|complete/.test(document.readyState)) {
this.ready(); // call the onload handler
}
}, 10);
}
/* for other browsers */
window.onload = this.ready;
}
}
this also solves cross browser issues ..
这也解决了跨浏览器问题..
回答by Tyler Rick
This is what I do (written in CoffeeScript):
这就是我所做的(用 CoffeeScript 编写的):
$(window).on 'load', ->
window._loaded = true
window.on_window_load_safe = (callback) ->
if window._loaded
# Since the load even already happened, if we tried to add a new callback now, it would not even
# get called. So just call the callback immediately now.
callback.call()
else
$(window).on 'load', ->
callback.call()
You can test it with something like this:
你可以用这样的东西来测试它:
setTimeout(->
on_window_load_safe ->
console.debug 'on_window_load_safe called me'
, 2000)
回答by Gaurav Agrawal
you can use
您可以使用
jQuery(document).ready();
at the end of the page or just before tag and it will call while your web page shows on client machine and approximately at the ending time of loading.....
在页面末尾或标记之前,它会在您的网页显示在客户端计算机上时调用,并且大约在加载结束时间.....