3 回答

TA貢獻1825條經驗 獲得超6個贊
這利用了DOMContentLoaded的優點-在onload之前會觸發-但允許您堅持所有不引人注意的內容...
window.onload-Dean Edwards-該博客文章討論了更多內容-這是從同一博客的評論中復制的完整代碼。
// Dean Edwards/Matthias Miller/John Resig
function init() {
// 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;
// kill the timer
if (_timer) clearInterval(_timer);
// do stuff
};
/* 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=javascript:void(0)><\/script>");
var 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
var _timer = setInterval(function() {
if (/loaded|complete/.test(document.readyState)) {
init(); // call the onload handler
}
}, 10);
}
/* for other browsers */
window.onload = init;

TA貢獻1829條經驗 獲得超7個贊
為什么不使用window自己的onload事件?
window.onload = function () {
alert("LOADED!");
}
如果我沒記錯的話,那么所有瀏覽器都兼容。

TA貢獻1871條經驗 獲得超8個贊
jcalfee314的想法對我有用-我有一個window.onload = onLoad含義,即其中的函數<body onload="...">沒有被調用(我無法控制)。
這修復了它:
oldOnLoad = window.onload
window.onload = onLoad;
function onLoad()
{
oldOnLoad();
...
}
編輯:Firefox不喜歡oldOnLoad = document.body.onload;,所以替換為oldOnLoad = window.onload。
添加回答
舉報