window.onload 加載完畢的問題及解決方案(下)
接上篇,其它方法:
一、在IE中還可以在onreadystatechange事件里進行判斷
http://www.thefutureoftheweb.com/blog/adddomloadevent
這里有Jesse Skinner寫了一段獨立的腳本函數(shù)來解決各種瀏覽器的onload問題,。
http://img.jb51.net/jslib/adddomloadevent.js
/*
* (c)2006 Jesse Skinner/Dean Edwards/Matthias Miller/John Resig
* Special thanks to Dan Webb's domready.js Prototype extension
* and Simon Willison's addLoadEvent
*
* For more info, see:
* http://www.thefutureoftheweb.com/blog/adddomloadevent
* http://dean.edwards.name/weblog/2006/06/again/
* http://www.vivabit.com/bollocks/2006/06/21/a-dom-ready-extension-for-prototype
* http://simon.incutio.com/archive/2004/05/26/addLoadEvent
*
*
* To use: call addDOMLoadEvent one or more times with functions, ie:
*
* function something() {
* // do something
* }
* addDOMLoadEvent(something);
*
* addDOMLoadEvent(function() {
* // do other stuff
* });
*
*/
addDOMLoadEvent = (function(){
// create event function stack
var load_events = [],
load_timer,
script,
done,
exec,
old_onload,
init = function () {
done = true;
// kill the timer
clearInterval(load_timer);
// execute each function in the stack in the order they were added
while (exec = load_events.shift())
exec();
if (script) script.onreadystatechange = '';
};
return function (func) {
// if the init function was already ran, just run this function now and stop
if (done) return func();
if (!load_events[0]) {
// for Mozilla/Opera9
if (document.addEventListener)
document.addEventListener("DOMContentLoaded", init, false);
// for Internet Explorer
/*@cc_on @*/
/*@if (@_win32)
document.write("<script id=__ie_onload defer src="http://0" src="http://0"><\/scr"+"ipt>");
script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
if (this.readyState == "complete")
init(); // call the onload handler
};
/*@end @*/
// for Safari
if (/WebKit/i.test(navigator.userAgent)) { // sniff
load_timer = setInterval(function() {
if (/loaded|complete/.test(document.readyState))
init(); // call the onload handler
}, 10);
}
// for other browsers set the window.onload, but also execute the old window.onload
old_onload = window.onload;
window.onload = function() {
init();
if (old_onload) old_onload();
};
}
load_events.push(func);
}
})();
二、另外還有在IE中的doScroll的,這是種方法只對IE有作用,而且它是一種hack方法。
在MSDN:About Element Behaviors 我們可以看到
When the ondocumentready event fires, the document has been completely parsed and built. Initialization code should be placed here if the component needs to navigate the primary document structure. The ondocumentready event notifies the component that the entire page is loaded, and it fires immediately before the onload event fires in the primary document.
A few methods, such as doScroll, require the primary document to be completely loaded. If these methods are part of an initialization function, they should be handled when the ondocumentready event fires.
http://javascript.nwbox.com/IEContentLoaded/
/*
*
* IEContentLoaded.js
*
* Author: Diego Perini (diego.perini at gmail.com) NWBOX S.r.l.
* Summary: DOMContentLoaded emulation for IE browsers
* Updated: 05/10/2007
* License: GPL/CC
* Version: TBD
*
*/
// @w window reference
// @fn function reference
function IEContentLoaded (w, fn) {
var d = w.document, done = false,
// only fire once
init = function () {
if (!done) {
done = true;
fn();
}
};
// polling for no errors
(function () {
try {
// throws errors until after ondocumentready
d.documentElement.doScroll('left');
} catch (e) {
setTimeout(arguments.callee, 50);
return;
}
// no errors, fire
init();
})();
// trying to always fire before onload
d.onreadystatechange = function() {
if (d.readyState == 'complete') {
d.onreadystatechange = null;
init();
}
};
}
在jQuery的源碼中,針對Mozilla, Opera 和webkit用的是DOMContentLoaded,也就是上一篇中第一種;
而對IE用的是doScroll的方法。
- 淺析document.ready和window.onload的區(qū)別講解
- JS:window.onload的使用介紹
- jquery $(document).ready() 與window.onload的區(qū)別
- jquery中的$(document).ready()與window.onload的區(qū)別
- js window.onload 加載多個函數(shù)的方法
- window.onload 加載完畢的問題及解決方案(上)
- 頁面中body onload 和 window.onload 沖突的問題的解決
- window.onload的頁面加載技巧
- window.onload綁定多個事件的兩種解決方案
相關文章
javascript之大字符串的連接的StringBuffer 類
javascript之大字符串的連接的StringBuffer 類...2007-05-05Javascript 獲取鏈接(url)參數(shù)的方法
Javascript 獲取鏈接(url)參數(shù)的實現(xiàn)方法2009-02-02BootStrap學習系列之Bootstrap Typeahead 組件實現(xiàn)百度下拉效果(續(xù))
這篇文章主要介紹了BootStrap學習系列之Bootstrap Typeahead 組件實現(xiàn)百度下拉效果的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07javascript 刪除數(shù)組中重復項(uniq)
巧妙去除數(shù)組中的重復項的方法參考,需要的朋友可以參考下。2010-01-01