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

Ticket #5414 (closed enhancement: fixed)

Opened 4 years ago

Last modified 3 years ago

[PATCH] Faster onload for Event.onload

Reported by: ross.lawley@gmail.com Assigned to: sam
Priority: high Milestone:
Component: Prototype Version:
Severity: normal Keywords: fixedinbranch 1.6
Cc: mislav, Tuxie, savetheclocktower, handcoding, haraldmartin

Description

Utilises faster onloading if the browser is capable.

Fully described here: http://www.agileweb.org/articles/2006/06/16/speeding-up-onload and based on Dean Edwards example

68,91d67
<   _observeLoad: function(element, name, observer, useCapture) {
<     var loader = function load() {
<       if (arguments.callee.done) return;
<       arguments.callee.done = true; 
<       if (_timer) {
<         clearInterval(_timer);
<         _timer = null;
<       }
<       observer();
<     }
<     if (document.addEventListener){
<       document.addEventListener("DOMContentLoaded", loader, null);
<     }
<     /*@cc_on @*/
<     /*@if (@_win32)
<         if (! $("__ie_onload"))
<           document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>"); 
<         var _timer = setInterval(function() {if (/loaded|complete/.test($("__ie_onload").readyState))loader();}, 10)
<     /*@end @*/
<     if (navigator.appVersion.match(/Konqueror|Safari|KHTML/i))
<         var _timer = setInterval(function() {if (/loaded|complete/.test(document.readyState))loader();},10);
<     this._observeAndCache(element, name, loader, useCapture);
<   },
< 
109,112c85,86
<     if (name == 'load')
<       this._observeLoad(element, name, observer, useCapture);
<     else    
<       this._observeAndCache(element, name, observer, useCapture);
---
>     
>     this._observeAndCache(element, name, observer, useCapture);

Attachments

eventPatch.diff (1.2 kB) - added by ross.lawley@gmail.com on 06/16/06 18:13:26.
diff file
eventPatch2.diff (1.2 kB) - added by Ross on 06/20/06 20:44:43.
updated Patch
eventPatch3.diff (1.4 kB) - added by ross on 06/26/06 17:11:08.
eventPatch4.diff (1.4 kB) - added by Ross on 06/27/06 12:36:55.
event.diff (2.5 kB) - added by ross on 09/05/06 15:21:58.
domReady Patch to event.js
domReady.html (3.6 kB) - added by ross.lawley@gmail.com on 09/05/06 15:22:53.
domReady - test / example
event.2.diff (2.4 kB) - added by ross.lawley@gmail.com on 09/07/06 11:18:33.
Event js updated

Change History

06/16/06 18:13:26 changed by ross.lawley@gmail.com

  • attachment eventPatch.diff added.

diff file

06/20/06 18:35:57 changed by martinstrom@gmail.com

Nice, but shouldn't it also check if the element is a window and not just if it's a load event? We want to use the old/regular way when doing things like:

var img = document.createElement("img");
Event.observe(img, 'load', function() {alert("image loaded")});
img.src = "...";

How do one check for window objects? Looking for some properties like innerWidth, location, status, open, resizeTo etc?

06/20/06 20:36:04 changed by Ross

Ha good point!

I've updated my demo here: http://www.agileweb.org/examples/prototypeOnLoad.html Which uses object extension to prototype rather than a patched prototype.

I changed the check to:

if (name == 'load' && element.screen)

element.screen should be available to all modern browsers: http://www.quirksmode.org/viewport/compatibility.html

I'll attach an updated diff.

06/20/06 20:44:43 changed by Ross

  • attachment eventPatch2.diff added.

updated Patch

06/26/06 17:11:08 changed by ross

  • attachment eventPatch3.diff added.

06/26/06 17:12:05 changed by ross

Added new patch - bug found in IE where not all dom content loaded before started processing.

Now fixed :)

06/27/06 12:36:55 changed by Ross

  • attachment eventPatch4.diff added.

06/27/06 12:41:02 changed by Ross

Added final patch - another IE test case showed if a very slow image was loaded it broke.

Have simplified the process away from using a setInterval in IE back to attaching an event to the script.readystatechange - to call the loader.

If you need test pages let me know - http://www.agileweb.org/examples/prototypeOnLoad.html

Shows it in use working, if your browser doesnt support one of the quicker load methods i.e. Opera 8 - then no harm is done as the default caller is still applied.

09/03/06 20:52:13 changed by madrobby

  • status changed from new to closed.
  • resolution set to untested.

I kind of like this, but this changes the behaviour of pages that rely on the window load event to only fire after page assets have loaded. How about adding a new event called domload? (maybe you can think of an even better name)

Also, this can be unit tested (by firing a normal onload event too, filling up an array). Feel free to reopen the ticket with the patch editing accordingly and unit tests attached.

09/05/06 15:20:45 changed by ross.lawley@gmail.com

  • status changed from closed to reopened.
  • resolution deleted.

Adding 'domReady' Event - seems to be the convetional name.

A diff will be attached and a test page - it broke the unit test script - so there are no unit tests (it returned a null error line 266 of unittest.js and I havent had time to debug that..)

09/05/06 15:21:58 changed by ross

  • attachment event.diff added.

domReady Patch to event.js

09/05/06 15:22:53 changed by ross.lawley@gmail.com

  • attachment domReady.html added.

domReady - test / example

09/06/06 21:14:51 changed by brandon.aaron@gmail.com

Perhaps the name should be case insensitive so that one could pass 'domready' or 'DOMReady' or 'domReady'.

Also I think the patch should use something like this snippet instead of conditional comments to avoid issues when the script is run through a compressor/compactor.

if (document.addEventListener)
  document.addEventListener("DOMContentLoaded", onDomReady, false);
else if (document.attachEvent && !window.opera) {
  document.write('<script id=__ie_onload defer src=://><\/script>');
  var script = document.getElementById('__ie_onload');
  script.onreadystatechange = function() {
    if (this.readyState == 'complete') onDomReady();
  };
}

09/07/06 11:17:35 changed by ross.lawley@gmail.com

Ok - taken on board and added case insensitivity for domReady.

Also shrunk the IE conditions, theres no need for the conditionals as if there isnt a domready equivalent then it falls through back to observing onload.

New diff file attached :)

09/07/06 11:18:33 changed by ross.lawley@gmail.com

  • attachment event.2.diff added.

Event js updated

10/09/06 10:59:34 changed by mislav

  • cc set to mislav.
  • keywords set to DOM onload.
  • priority changed from normal to high.

10/09/06 18:47:43 changed by Tuxie

  • cc changed from mislav to mislav, Tuxie.

(follow-up: ↓ 14 ) 12/05/06 23:05:22 changed by savetheclocktower

  • cc changed from mislav, Tuxie to mislav, Tuxie, savetheclocktower.

I agree with Thomas about how replacing "load" isn't necessarily the best course of action. On the other hand, I think having a different function altogether (Event.domReady) is inconsistent.

How about we call it "DOMContentLoaded"? It is the standard, after all (DOM Level 3 Events), so we might as well start calling it that. That way, once all browsers support DOMContentLoaded natively (i.e., 89068903 years from now), we'd be able to remove all the special code without breaking backwards-compatibility.

(in reply to: ↑ 13 ) 12/05/06 23:11:50 changed by mislav

Replying to savetheclocktower:

How about we call it "DOMContentLoaded"?

That is absolutely brilliant

02/28/07 23:13:03 changed by handcoding

  • cc changed from mislav, Tuxie, savetheclocktower to mislav, Tuxie, savetheclocktower, handcoding.

03/01/07 08:46:51 changed by haraldmartin

  • cc changed from mislav, Tuxie, savetheclocktower, handcoding to mislav, Tuxie, savetheclocktower, handcoding, martinstrom@gmail.com.

03/01/07 08:47:12 changed by haraldmartin

  • cc changed from mislav, Tuxie, savetheclocktower, handcoding, martinstrom@gmail.com to mislav, Tuxie, savetheclocktower, handcoding, haraldmartin.

04/27/07 22:35:16 changed by mislav

Hello subscribers. You'll like [6596] that happened in the event branch just now :)

It'll be in trunk soon.

06/14/07 01:13:22 changed by Tobie

  • keywords changed from DOM onload to fixedinbranch 1.6.

08/07/07 13:32:31 changed by madrobby

  • status changed from reopened to closed.
  • resolution set to fixed.

Support for this was added to trunk in [7270].