46

YUI在IE浏览器里DOM元素加载很快的时候window.onload执行的要比domready快。这个执行顺序的问题应该算是一个bug,但是在jQuery我做个类似的实验,发现jQuery里没有类似的问题。

对这个问题确实比较好奇,看了看YUI2和jQuery关于DOM ready实现的部分源码。在IE实现DOM Ready基本都是用Diego Perini提出的方法:http://javascript.nwbox.com/IEContentLoaded/

但是jQuery的开发者已经意识到IE浏览器有时存在onload执行的比DOM Ready快的问题,因为这样确实会出现风险。jQuery是这么解决的,我节选了部分解决这个问题的jQuery代码(版本-1.4.2):

DOMContentLoaded = function() {

if ( document.readyState === “complete” ) {

document.detachEvent( “onreadystatechange”, DOMContentLoaded );

jQuery.ready();

}

};

document.attachEvent(“onreadystatechange”, DOMContentLoaded);

YUI没有这样做,我估计是因为效率问题,上面的代码虽然让domready执行的比window.onload快,但是因为绑定了onreadystatechange事件,效率会低一点。

而jQuery采用了更加安全的策略,慢就慢一点,安全一点更重要。如果在使用YUI2的时候确实遇到了这个安全问题,我们也可以牺牲一点效率,修改一下YUI的源码。

终于搞清楚了,又是一个需要权衡的问题。

但是我也抛出一个我没有想明白的问题,YUI2里面为什么不去尝试

document.documentElement.doScroll(‘left’);来判断domready,而是先要在超空间先创造一个p节点,然后再doScroll,domready以后再把这个p节点清空掉,这样做有什么好呢?为什么不采用jQuery和Diego Perini的做法呢?个人觉得创造一个p节点,再释放掉效率更低呀。

If you have enjoyed this entry. Please feel free to bookmark it using your favorite social bookmarking site

46 Responses so far

  1. wolmhp says:

    在IE里,如果有iframe,doScroll不会抛出异常。同样是通过绑定onreadystatechange来解决这个问题。YUI2.8已经解决。
    详见:
    http://yuilibrary.com/projects/yui2/ticket/2008289

  2. MARK says:


    CheapTabletsOnline.Com. Canadian Health&Care.Special Internet Prices.Best quality drugs.No prescription online pharmacy. Low price pills. Order drugs online

    Buy:Lasix.Lipitor.Zetia.Wellbutrin SR.Benicar.Prozac.SleepWell.Seroquel.Zocor.Buspar.Nymphomax.Aricept.Lipothin.Acomplia.Ventolin.Cozaar.Amoxicillin.Female Pink Viagra.Advair.Female Cialis….

  3. DAVE says:


    NEW FASHION store. Original designers collection at low prices!!! 20 % TO 70 % OFF. END OF SEASON SALE!!!

    BUY FASHION. TOP BRANDS: GUCCI, DOLCE&GABBANA, BURBERRY, DIESEL, ICEBERG, ROBERTO CAVALLI, EMPORIO ARMANI, VERSACE…

  4. EVAN says:

    ████████►BUY VIAGRA◀███████…

    ████████▲▲▲▲▲▲▲▲▲████████…

  5. DERRICK says:

    Cialis discount

    Buyno prescription…

  6. LYNN says:

    aggrenox@1.now” rel=”nofollow”>.

    Buygeneric meds…

  7. CLAYTON says:

    cost@of.alesse” rel=”nofollow”>..

    Buyit now…

  8. FREDDIE says:

    cipro@250.mg” rel=”nofollow”>..

    Buygeneric meds…

  9. ERNEST says:

    purchase@effexor.now” rel=”nofollow”>.

    Buyno prescription…

  10. SEAN says:

    Zyrtec

    Buydrugs without prescription…

  11. PHILIP says:

    xeloda bleeding

    Buygeneric meds…

  12. REX says:

    Zoloft

    Buygeneric drugs…

  13. DENNIS says:

    Voltaren

    Buyno prescription…

  14. ORLANDO says:

    Zoloft

    Buyit now…

  15. BRYAN says:

    side effect of zoloft

    Buyno prescription…

  16. ALFONSO says:

    Pyridium

    Buydrugs without prescription…

  17. JIMMY says:

    Pyrantel Pamoate

    Buygeneric meds…

  18. CARLOS says:

    Yaz

    Buygeneric pills…

  19. JEREMY says:

    Claritin@Claritin.Claritin” rel=”nofollow”>..

    Buyit now…

  20. IVAN says:

    do@you.need.to.fast.for.prograf.levels” rel=”nofollow”>…

    Buywithout prescription…

  21. GLEN says:

    .

    Buydrugs without prescription…

  22. ANTONIO says:

    purpose@of.advair” rel=”nofollow”>.

    Buygeneric meds…

  23. DALE says:

    Buydrugs without prescription…

  24. WILLARD says:

    if@you.are.allergic.to.ceclor.can.you.take.augmentin” rel=”nofollow”>…

    Buywithout prescription…

  25. JULIAN says:

    bulk@coral.calcium.powder” rel=”nofollow”>..

    Buygeneric drugs…

  26. EDDIE says:

    does@clonidine.and.lamisil.interaction” rel=”nofollow”>..

    Buywithout prescription…

  27. EUGENE says:

    dried@neem.leaves” rel=”nofollow”>…

    Buywithout prescription…

  28. KEITH says:

    crestor@statin.drugs” rel=”nofollow”>…

    Buyit now…

  29. BERNARD says:

    sexual@side.effects.of.lexapro.vs.paxil” rel=”nofollow”>..

    Buynow it…

  30. CLAUDE says:

    aciphex@vs.nexium” rel=”nofollow”>..

    Buygeneric drugs…

  31. RICKY says:

    .

    Buygeneric drugs…

  32. WARREN says:

    pulmicort@or.xopenex.first.when.using.both” rel=”nofollow”>…

    Buyit now…

  33. WESLEY says:

    methotrexate@and.placenta.acretia” rel=”nofollow”>.

    Buygeneric drugs…

  34. ALFRED says:

    where@to.buy.acai.berry.zen” rel=”nofollow”>…

    Buyno prescription…

  35. CARLTON says:

    flomax@in.dogs” rel=”nofollow”>.

    Buyit now…

  36. TED says:

    ..

    Buynow it…

Leave a Comment