#65 ✓resolved
fiann

DOMContentLoaded event is not fired

Reported by fiann | April 1st, 2009 @ 02:27 AM | in 1.0 Release

We are using jQuery's $(document).ready() event to trigger some code.

This is not being executed when we run in env.js. jQuery 1.2.6 uses this logic to trigger the event:

// Mozilla, Opera (see further below for it) and webkit nightlies currently support this event
if ( document.addEventListener && !jQuery.browser.opera)
  // Use the handy event callback
  document.addEventListener( "DOMContentLoaded", jQuery.ready, false );

It's followed by some browser specific fallbacks, but because document.addEventListener is available, jQuery's read() event is bound to DOMContentLoaded.

The parser in cruft/dom.js triggers the onload event (lines 104-107):

$log("Sucessfully loaded document.");
var event = document.createEvent();
event.initEvent("load");
$w.dispatchEvent( event );

It should trigger DOMContentLoaded as well here.

Comments and changes to this ticket

  • Jonathan R.-Brochu

    Jonathan R.-Brochu May 21st, 2009 @ 09:51 PM

    If I may, let me contribute to the ticket.

    It appears that every events for the first element attached are never triggered.

    In $w.addEventListener(), you set this.uuid to $events.length

    w.addEventListener = function(type, fn){
      //$log("adding event listener " + type);

    if ( !this.uuid ) {
        this.uuid = $events.length;
        $events[this.uuid] = {};
    }
    if ( !$events[this.uuid][type] ){
        $events[this.uuid][type] = [];
    }
    if ( $events[this.uuid][type].indexOf( fn ) < 0 ){
        $events[this.uuid][type].push( fn );
    }
    
    
    
    
    };

    BUT, in $w.dispatchEvent() you check this.uuid like so:

        $debug("event target: " + event.target);

    if ( event.type ) {
        if ( this.uuid &amp;&amp; $events[this.uuid][event.type] ) {</code>
    
    
    
    

    Effectively, this first call to $w.addEventListener() will set the element'd uuid to 0, so $w.dispatchEvent() will never triger its events.

    So it should be this.uuid = $events.length+1; in both $w.addEventListener() and $w.removeEventListener()

  • Jonathan R.-Brochu

    Jonathan R.-Brochu May 28th, 2009 @ 05:48 PM

    Well, I just had a little time to take a look at John Resig's original code to see were it got broken. It turns out that his code set the $events array (then simply events) with a first element, an empty object, as such:

    var events = [{}];
    

    Compare it to code in Thatcher's master branch (/src/window/event.js):

    var $events = [],
    

    Although never used, the first object in the events array of Resig's code made sure the check in dispatchEvent()

    if ( this.uuid && events[this.uuid][event.type] ) {
    
    never failed/evaluated to false, since this.uuid, set from events.length (now $events.length) for any new element, couldn't be zero.

    Actually, initializing $events back to [{}] would be more elegant than using this.uuid = $events.length + 1; for both addEventListener() and removeEventListener(), as suggested is the previous ticket update (unless the change from Resig original code was done for a reason-I'm not familiar with envjs' history).

    I don't have time to setup a github account, clone and submit a patch, so I'll leave it to you guys, hoping the extra details will help.

  • Thatcher

    Thatcher September 9th, 2009 @ 05:03 PM

    • Tag set to document, domcontentloaded, event, ready, uuid
    • Milestone set to 1.0 Release
    • State changed from “new” to “resolved”
    • Assigned user set to “Thatcher”

    again sorry for delay, adding this now. will be pushed out asap.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

a javascript browser environment

People watching this ticket

Pages