返回列表

jQuery实现$(document).ready(function(){alert(123); })的代码

默认分类 2011-03-27 20:02:03

  1. bindReady:function() { 
  2.    if (readyBound) { 
  3.        return
  4.     } 
  5.     readyBound =true
  6.    if (document.readyState === "complete") { 
  7.        return setTimeout(jQuery.ready, 1); 
  8.     } 
  9.    if (document.addEventListener) { 
  10.         document.addEventListener("DOMContentLoaded", DOMContentLoaded, false); 
  11.         window.addEventListener("load", jQuery.ready, false); 
  12.     }elseif (document.attachEvent) { 
  13.         document.attachEvent("onreadystatechange", DOMContentLoaded); 
  14.         window.attachEvent("onload", jQuery.ready); 
  15.        var toplevel = false
  16.        try
  17.             toplevel = window.frameElement ==null
  18.         }catch(e) {} 
  19.        if (document.documentElement.doScroll && toplevel) { 
  20.             doScrollCheck(); 
  21.         } 
  22.     } 
  23.  
  24. function doScrollCheck() { 
  25.    if (jQuery.isReady) { 
  26.        return
  27.     } 
  28.    try
  29.         // If IE is used, use the trick by Diego Perini; 
  30.         // http://javascript.nwbox.com/IEContentLoaded/ 
  31.         document.documentElement.doScroll("left"); 
  32.     }catch(e) { 
  33.         setTimeout(doScrollCheck, 1); 
  34.        return
  35.     } 
  36.     jQuery.ready(); 
转载:http://javascript.nwbox.com/IEContentLoaded/

IEContentLoaded (last updated 05/10/2007)

An alternative for DOMContentLoaded on Internet Explorer

This is an Internet Explorer only method. It does not produce the expected results on other browsers.
It is meant to be used as an IE alternative in other fine scriptstrying to fix the window onload problem.
It is based on a hack (no doubt about that), but I hope it is an improvement over previous IE hacks.

Several tests has showed that the only other (documented)method covering all the aspects of this complex problem is the original Microsoft HTC method as described by Dean Edwards in An Alternative Solution.

However an inline solution is always preferred, possibly XHTML compatible, with no document.write and without requiring external files.

The solution I show here uses the "doScroll()" method in a try/catch polling loop. This triggers when no errors are returned by "doScroll().

You can quickly download the small iecontentloaded.jsscript or just have a lookat it.

The Microsoft online documentation About Element Behaviors tell us something about this:

When the ondocumentready event fires, the document has been completely parsed and built. Initialization code should be placed here if the component needs to navigate the primary document structure. The ondocumentready event notifies the component that the entire page is loaded, and it fires immediately before theonload event fires in the primary document.

A few methods, such as doScroll, require the primary document to be completely loaded. If these methods are part of an initialization function, they should be handled when the ondocumentreadyevent fires.

Some TEST CASES showing the IEContentLoaded method

The objective of these tests is to compare the detection reliability of this method.

In this specific case we are interested in determining when all the nodes in the
requested page have been loaded and parsed in the DOM and available to scripts.

The fact that images exists or not in the page shouldn't affect this test.

The test case executes these steps, in sequence, at different times:

  1. INLINE STYLE is applied and the BODY background color is set to "lime"
  2. Ending BODY SCRIPT is executed and the background color is set to "red"
  3. IEContentLoaded function is fired and the background color is set to "green"

The page should have a "green" background color after being loaded completely.
If that is not the case then something went wrong in my script or in your browser.

The IEContentLoaded source code

  1. /*
  2.  *
  3.  * IEContentLoaded.js
  4.  *
  5.  * Author: Diego Perini (diego.perini at gmail.com) NWBOX S.r.l.
  6.  * Summary: DOMContentLoaded emulation for IE browsers
  7.  * Updated: 05/10/2007
  8.  * License: GPL/CC
  9.  * Version: TBD
  10.  *
  11.  */ 
  12.  
  13. // @w   window reference 
  14. // @fn  function reference 
  15. function IEContentLoaded (w, fn) { 
  16.    var d = w.document, done =false
  17.    // only fire once 
  18.     init =function () { 
  19.        if (!done) { 
  20.             done = true
  21.             fn(); 
  22.         } 
  23.     }; 
  24.    // polling for no errors 
  25.     (function () { 
  26.        try
  27.            // throws errors until after ondocumentready 
  28.             d.documentElement.doScroll('left'); 
  29.         } catch (e) { 
  30.             setTimeout(arguments.callee, 50); 
  31.            return
  32.         } 
  33.        // no errors, fire 
  34.         init(); 
  35.     })(); 
  36.    // trying to always fire before onload 
  37.     d.onreadystatechange =function() { 
  38.        if (d.readyState == 'complete') { 
  39.             d.onreadystatechange =null
  40.             init(); 
  41.         } 
  42.     }; 

Fine script for the window.onload problem