今天查看百度空间源代码,发现多了个util.js文件,打开看看。里面里面定义了addDOMLoadEvent。这是干什么用的?

仔细查看代码,发现在Mozilla添加了DOMContentLoaded事件,这个在以前一直没有用过。                    if (document.addEventListener)
                       
document.addEventListener("DOMContentLoaded", init, false);

好久就是为了兼容实现DOMContentLoaded事件。

网上找了点有关DOMContentLoaded的资料拿来看看。

DOMContentLoaded是firefox下特有的Event, 当所有DOM解析完以后会触发这个事件。
   
与DOM中的onLoad事件与其相近。但onload要等到所有页面元素加载完成才会触发, 包括页面上的图片等等。
    如果页面的图片很多的话,
从用户访问到onload触发可能需要较长的时间, 而在Ajax运用中, 常常需要在onload中加入许多初始化的动作, 如果由于网络问题引起的图片加载过慢(
见: Ajax优化(2) -- lazierLoad img && js), 则必然影响用户的体验。
   
在这种情况下firefox的DOMContentLoaded事件, 恰恰是我们需要的。

目前,跨平台的DOMContentLoaded的解决方案有很多, 比如jQuery, Prototype...等等, 其实现原理大同小异.

在项目中, 我使用了Prototype工具, 以往调用初始化的方法是:

Event.observe(window,
"load", init);

现在有了DOMContentLoaded, 可以替换成如下的方法:

document.observe('contentloaded',
init);

最新的prototype中自定义事件已经重新命名, 使用"dom:loaded" 代替了 “contentloaded”.

document.observe('dom:loaded',
init);

附:
   Andrea Giammarchi
的OnContent函数提供了一个跨平台的DOMContentLoaded的解决方案My DOMContentLoaded
Final Solution

文件名称:DOMContentLoaded.js

function onContent ( f ) {

var a = onContent,

b = navigator . userAgent ,

d = document ,

w = window ,

c = "onContent" ,

e = "addEventListener" ,

o = "opera" ,

r = "readyState" ,

s = "<scr" . concat ( "ipt defer src='//:' on" ,
r, "change='if(this." ,
r, "==\"complete\"){this.parentNode.removeChild(this);"
, c, "." , c, "()}'></scr" , "ipt>" ) ;

a[ c] = ( function ( o) {

return function ( ) {

a[ c] = function ( ) { } ;

for ( a = arguments . callee ; ! a. done; a. done = 1) f( o ? o( ) : o)

}

} ) ( a[ c] ) ;

if ( d[ e ] ) d[ e ] ( "DOMContentLoaded" , a[ c] , false ) ;

if ( / WebKit| Khtml/ i. test ( b) | | ( w[ o] & & parseInt ( w[ o] . version ( ) ) < 9) ) ( function ( ) { / loaded | complete / . test ( d[ r] ) ? a[ c] ( ) : setTimeout ( arguments . callee , 1)

} ) ( ) ;

else if ( / MSIE/ i. test ( b) ) d. write ( s) ;

  1. } ;

util.js:

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())
                    setTimeout(exec,
10);
                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=//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);
        }
})();

function insertWBR(string, step){
    var textarea =
document.createElement('TEXTAREA');
    textarea.innerHTML =
string.replace(/</g,"&lt;").replace(/>/g,"&gt;");
    string =
textarea.value;

var step = (step || 5), reg = new RegExp("(\\S {" + step + "})",
"gi");
    return
string.replace(/(<[^>]+>)/gi,"$1<wbr/>").replace(/(>|^)([^<]+)(<|$)/gi,
function(a,b,c,d){
        if(c.length < step) return a;
        return
b + c.replace(reg, "$1<wbr/>") + d;
    });
}

DOMContentLoaded事件的更多相关文章

  1. Window.onLoad 和 DOMContentLoaded事件的先后顺序

    相信写js的,都知道window.onload吧,但是并不是每个人都知道DOMContentLoaded,其实即使你不知道,很有可能你也经常使用了这个东西. 一般情况下,DOMContentLoade ...

  2. 模拟DOMContentLoaded事件

    window.onload事件 文档中所有图片,脚本,链接以及子框完成加载后,才会触发window.onload事件. 浏览器兼容性:All DOMContentLoaded事件 当页面中的文档树解析 ...

  3. JS/CSS/IMG加载顺序关系之DOMContentLoaded事件

    DOMContentLoaded介绍 DOMContentLoaded事件的触发条件是: 将会在“所有的DOM全部加载完毕并且JS加载执行后触发”. 但如果“js是通过动态加载进来的话,是不会影响到D ...

  4. HTML5-常见的事件- DOMContentLoaded事件

    一般我们监听文档是否加载完成是使用 window的load事件,该事件会在页面中的一切加载完毕时触发,但这个过程可能会因为要加载的外部资源过多而等待时间过长. DOMContentLoaded事件:则 ...

  5. HTML load事件和DOMCOntentLoaded事件

    JS高程 p14 “异步脚本一定会在页面的load事件前执行,但可能会在DOMContentLoaded事件触发之前或之后执行”   普通script标签会阻塞DOM的解析 DOMcontentLoa ...

  6. DOMContentLoaded事件中使用异步

    概述 我在之前的博文(Performance面板看js加载)中提到过,如果利用监听DOMContentLoaded事件的方式来加载js是不能优化加载的,不能够替代jquery中的ready方法,原因是 ...

  7. JS、CSS以及img对DOMContentLoaded事件的影响

    最近在做性能有关的数据上报,发现了两个非常有意思的东西:Chrome开发者工具的Timeline分析面板,以及DOMContentLoaded事件.一个是强大的令人发指的性能分析工具,一个是重要的性能 ...

  8. load/domContentLoaded事件、异步/延迟Js 与DOM解析

    一.DOMContentLoaded 与 load事件 关于load和DOMContentLoaded事件,mdn对于它们是这样描述的: DOMContentLoaded mdn文档地址:https: ...

  9. 深入理解Javascript封装DOMContentLoaded事件

    最近在写一个Javascript的框架,刚把DOMContentLoaded事件封装好,略带小兴奋,把开发过程中遇到的原理和兼容性问题做篇笔记,省的忘记到处找. 我们在写js代码的时候,一般都会添加w ...

随机推荐

  1. 【转】VC++与MySQL数据库的连接

    原文地址:http://blog.csdn.net/nupt123456789/article/details/8043091 1.MySQL数据库的安装 你可以从MySQL的官网上或者从如下地址下载 ...

  2. lintcode :最长公共子串

    题目 最长公共子串 给出两个字符串,找到最长公共子串,并返回其长度. 样例 给出A=“ABCD”,B=“CBCE”,返回 2 注意 子串的字符应该连续的出现在原字符串中,这与子序列有所不同. 解题 注 ...

  3. JavaScript的基础语法,你真的了解吗?

    这篇文章是在我们熟悉了JS的基础语法后,很少有人去关注的一些细节部分.如果掌握了某些细节也许会对代码的改善有着非凡的作用.也许会使我们的代码更严谨,更高效. 1.if语句的条件 if条件中,括号里是布 ...

  4. mmap 的理解

    mmap 的理解 采用共享内存通信的一个显而易见的好处 是效率高,因为进程可以直接读写内存,而不需要任何数据的拷贝.对于像管道和消息队列等通信方式,则需要在内核和用户空间进行四次的数据拷贝,而共享内存 ...

  5. Linux Shell学习

    https://yunpan.cn/cMxw3i8TkcsWI (提取码:d4e1)

  6. iOS 开发压缩--SSZipArchive

    // 解压 NSString *zipPath = @"被解压的文件路径"; NSString *destinationPath = @"解压到的目录"; [S ...

  7. JLink v8克隆版破解向导(此方法仅适用XP32位版 WIN7及以上和64位均不支持 建议使用虚拟机)

    此方法仅适用XP32位版 WIN7及以上和64位均不支持 建议使用虚拟机 摘要 Jlink 4.5版本之后驱动会识别老的克隆版的JlinkV8,Jlink软件在启动时会提示为克隆版本后退出.目前主流的 ...

  8. 拥有更好性能的requesAnimationFrame(Better Performance with requestAnimationFrame)

    介绍: 这篇文章讨论的是你可以(也应该)学习通过使用requestAnimationFrame API,而不是使用之前的setInterval/setTimeout方法,来提高动画的性能:如何使用re ...

  9. mars android视频学习笔记一:Activity生命周期

    (1)创建:onCreate->onStart->onResume;(2)失去焦点:onPause->onStop:(3)重新获得焦点:onRestart->onStart-& ...

  10. Netty4.x中文教程系列(一) 目录及概述

    Netty4.x中文教程系列(一)目录及概述 Netty 提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服务器和客户端程序. Netty是一个NIO客户端 服务端框架 ...