今天查看百度空间源代码,发现多了个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. C#加密算法汇总

    方法一: //须添加对System.Web的引用 using System.Web.Security; ... /// <summary> /// SHA1加密字符串 /// </s ...

  2. PHP组合模式、策略模式

    一.问题 模拟不同课程有不同的收费方式,并且能灵活改变(新增或删减),如讲座可以固定收费也可改为按时收费,研讨会也是. 二.模式简介及关键点 1.在父类代码中使用条件语句是一种退倒,可以用多态来代替条 ...

  3. Linux系统添加硬盘设备(磁盘分区-格式化-挂载-使用)

    当全新安装了一块新的硬盘设备后,为了更充分.更安全的利用硬盘空间首先要进行磁盘的分区, 然后格式化,最后挂载使用. 实例:对新添加的硬盘设备进行分区.格式化并挂载到/newFS目录. 第一步:在vmw ...

  4. intelliJ IDEA中项目以jar包的形式导出

    在上一篇中把intelliJ IDEA安装并配置完事后,我们就可以写scala程序了.编写完scala程序后我们要把程序导出,上传到服务器上,在spark集群下运行,下面就讲一下包的导出过程以及包在服 ...

  5. 如何写计算机会议的rebuttal

    其实最好的教材就是实例,恰好NIPS会议会把往年所有论文的Rebuttal都贴出来...,见这里:http://papers.nips.cc/ 同时,圈内同行也总结了不少经验,下面转帖其他人的经验 = ...

  6. c# 任意多个数,求最大值

    c#  任意多个数,求最大值 使用parms: 正在研究中,如果有好的方案,可评论,共同进步,共同提高,谢谢!

  7. ubuntu 默认 进入 命令行

    图形模式下,首先进入终端:1. 运行 sudo vi/etc/default/grub2. 找到 GRUB_CMDLINE_LINUX_DEFAULT=”quiet splash”3.改为 GRUB_ ...

  8. C# 中通过API实现的打印类

    using System;using System.Collections;using System.Text;using System.Runtime.InteropServices; using ...

  9. HttpClient 4.x 执行网站登录并抓取网页的代码

    HttpClient 4.x 的 API 变化还是很大,这段代码可用来执行登录过程,并抓取网页. HttpClient API 文档(4.0.x), HttpCore API 文档(4.1) pack ...

  10. Create Entity Data Model

    http://www.entityframeworktutorial.net/EntityFramework5/create-dbcontext-in-entity-framework5.aspx 官 ...