Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6596

Show
Ignore:
Timestamp:
04/27/07 22:31:13 (2 years ago)
Author:
mislav
Message:

Support for "DOMContentLoaded" event handling. Usage:

Event.observe(document, 'DOMContentLoaded', callback);

Based on work by Dan Webb, Matthias Miller, Dean Edwards and John Resig.
Functional test included (tested in FF and IE6/Linux for now). Shouldn't
have memleaks. Fixes #5414

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • spinoffs/prototype/branches/event/src/event.js

    r6537 r6596  
    1 if (!window.Event) { 
    2   var Event = new Object(); 
    3 
     1if (!window.Event) var Event = {}; 
    42 
    53Object.extend(Event, { 
     
    9391    }, 
    9492    add: function() { 
     93      if (this.type == 'DOMContentLoaded' && this.element == document) { 
     94        onReady(this.observer); 
     95        return; 
     96      } 
    9597      this._add(); 
    9698      this.cache(); 
     
    186188    return true; // the calling method can end 
    187189  }; 
     190 
     191  // based on work by Dan Webb, Matthias Miller, Dean Edwards and John Resig 
     192  var readyCallbacks, timer; 
     193 
     194  function domReady() { 
     195    if (arguments.callee.done) return; 
     196    arguments.callee.done = true; 
     197    if (timer) clearInterval(timer); 
     198     
     199    readyCallbacks.each(function(f) { f() }); 
     200    readyCallbacks = null; 
     201  } 
     202  function onReady(f) { 
     203    if (!readyCallbacks) { 
     204      if (domReady.done) return f(); 
     205       
     206      if (document.addEventListener) { 
     207        if (B.WebKit) {  
     208          timer = setInterval(function() { 
     209            if (/loaded|complete/.test(document.readyState)) domReady();  
     210          }, 10); 
     211        } 
     212        else document.addEventListener('DOMContentLoaded', domReady, false); 
     213      } 
     214      else if (B.IE) { 
     215        var dummy = location.protocol == "https:" ? "https://javascript:void(0)" : "javascript:void(0)"; 
     216        document.write("<script id=__ie_onload defer src='" + dummy + "'><\/script>"); 
     217        $("__ie_onload").onreadystatechange = function() { if (this.readyState == "complete") { 
     218          this.onreadystatechange = null; domReady(); 
     219        }}; 
     220      } 
     221       
     222      Event.observe(window, 'load', domReady); 
     223      readyCallbacks = []; 
     224    } 
     225    readyCallbacks.push(f); 
     226  } 
    188227   
    189228  return { 
  • spinoffs/prototype/branches/event/test/functional/event.html

    r6462 r6596  
    4444  <script type="text/javascript"> Element.addMethods(); </script> 
    4545   
    46   <script src="../lib/unittest.js" type="text/javascript"></script> 
    47   <link rel="stylesheet" href="../test.css" type="text/css" /> 
    48    
    4946  <style type="text/css" media="screen"> 
    5047  /* <![CDATA[ */ 
    51     body { margin:1em 2em; padding:0
     48    body { margin:1em 2em; padding:0; font-size:0.8em
    5249    hr { width:31.2em; margin:1em 0; text-align:left } 
    5350    p { width:30em; margin:0.5em 0; padding:0.3em 0.6em; color:#222; background:#eee; border:1px solid silver; }