Rob*_*cks 37 html javascript events onload
如何以交叉浏览器方式将身份onload事件与JS相关联?这么简单吗?
document.body.onload = function(){
alert("LOADED!");
}
Run Code Online (Sandbox Code Playgroud)
gna*_*arf 22
这利用了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;
Run Code Online (Sandbox Code Playgroud)
And*_*ech 21
为什么不使用window
自己的onload
活动?
window.onload = function () {
alert("LOADED!");
}
Run Code Online (Sandbox Code Playgroud)
如果我没有弄错的话,这在所有浏览器中都是兼容的.
Aam*_*idi 14
跨浏览器window.load事件
function load(){}
window[ addEventListener ? 'addEventListener' : 'attachEvent' ]( addEventListener ? 'load' : 'onload', load )
Run Code Online (Sandbox Code Playgroud)
document.body.onload
是一种跨浏览器,但是只允许单个回调的传统机制(您无法为其分配多个功能).
addEventListener
Internet Explorer(它使用attachEvent
)不支持最接近的"标准"替代方案,因此您可能希望使用库(jQuery,MooTools,prototype.js等)来为您抽象跨浏览器的丑陋.
归档时间: |
|
查看次数: |
111395 次 |
最近记录: |