Anyone Else Interested in a New Synthesized "onReady" Event (ala JQuery)?

696 views
Skip to first unread message

machineghost

unread,
May 6, 2008, 5:47:05 PM5/6/08
to MochiKit
Hey All,

In trying to explain why he liked jQuery, a co-worker of mine clued me
in to a fairly cool method in that library: "ready(someFunc);". For
those who aren't familiar with jQuery, you can think of this method as
something like:
partial(connect, document, "DOMContentLoaded")

In other word, it connects the provided function to the
"DOMContentLoaded" document event. Now, the specific jQuery syntax I
could care less about (with partial I can already make any "connect"
variant I want), but what is really cool about the function is that it
makes it really easy to use the "DOMContentLoaded" event. And why is
that cool?

1) The "DOMContentLoaded" event fires sooner (and if you have a heavy
page, MUCH sooner) than the "onLoad" event (although you can't do
stuff that depends on the rendered DOM, like getElementDimensions,
until "onLoad" goes off). As a result, you can get significant
performance improvements just by changing (most of) your "onLoad" code
to be "onDOMContentLoaded" code.

2) Internet Explorer doesn't support "DOMContentLoaded" :-( Luckily
however, this guy:
http://javascript.nwbox.com/IEContentLoaded/
figured out a hack to emulate the event in IE. When you call
"ready(someFunc)", behind the scenes JQuery handles figuring out
whether to use the hack or the real event.

Now, I don't have any desire to switch to jQuery; it has the same
basic stuff as Mochikit, but none of the wonderful advanced stuff like
partial, keys, etc. I would however like to have access to this
"fake" event. Am I alone in this, or would others on this list like
to see a synthesized Mochikit "DOMContentLoaded" event (similar to the
existing synthesized "onmouseenter" and "onmouseleave" events)?

If there is interest in this event I'll be happy to help write a
proper Mochikit patch, but if not I'll just steal (quick and dirty
style) the jQuery event for my own purposes.

Felipe Alcacibar B

unread,
May 7, 2008, 12:12:42 AM5/7/08
to MochiKit
MochiKit is wonderful, as you wrote ir, but is the base, you can made
too much things, but you may need read or investigate some more time
like jQuery.

I prefer call it domready, because it is when dom nodes are loaded, i
use this code to implement domready, and i not have any problem.

[code]
var __domready__ = false;
if (document.addEventListener)
document.addEventListener("DOMContentLoaded", function() { if(!
__domready__) { __domready__ = true; window.signal(window,
'ondomready') }; }, false);

/*@cc_on @*/
/*@if (@_win32)
document.write("<script id=__ie_onload defer
src=javascript:void(0)><\/script>");
document.getElementById("__ie_onload").onreadystatechange =
function() {
if (this.readyState == "complete") {
signal(window, 'ondomready');
}
};
/*@end @*/

if (/WebKit/i.test(navigator.userAgent)) {
var __domready__timer__ = setInterval(function() {
if (/loaded|complete/.test(document.readyState)) signal(window,
'ondomready');
clearInterval(ctimer);
}, 10);
}
[/code]

and when i need to call them i use

[code]
connect(window, 'ondomready', function () {
alert('now i know kung fu');
});
[/code]


Felipe Alcacibar Buccioni
Developer of systems and solutions

Felipe Alcacibar B

unread,
May 7, 2008, 12:21:50 AM5/7/08
to MochiKit
oops, some mistake in the webkit part, sorry

[code]
if (/WebKit/i.test(navigator.userAgent)) {
var __domready__timer__ = setInterval(function() {
if (/loaded|complete/.test(document.readyState)) signal(window,
'ondomready');
clearInterval(__domready__timer__);
}, 10);
[/code]

Per Cederberg

unread,
May 7, 2008, 1:51:01 AM5/7/08
to MochiKit
I liked the "ondomready" name and structure. Looking at MSDN, they
provide the following solution instead of writing new script tags into
the document (not tested, just pasted):

document.onreadystatechange=fnStartInit;
function fnStartInit()
{
if (document.readyState=="complete")
{
// Finish initialization.
}
}

Otherwise I like the proposed solutions and vote for inclusion to
MochiKit.Signal. Never know when it might be handy.

Cheers,

/Per

Bob Ippolito

unread,
May 7, 2008, 1:58:28 AM5/7/08
to Per Cederberg, MochiKit
This would definitely be convenient, it's always been on my list and
I've hacked together crappy (polling) implementations once or twice,
but I never needed it bad enough to tackle all of the cross-browser
issues myself :)

machineghost

unread,
May 7, 2008, 6:57:04 PM5/7/08
to MochiKit
Between the feedback on this list and the feedback I got offline (one
"Oh god, yes." email) it seems that there definitely is an interest
for an "ondomready" synthesized event. As soon as I can find the time
I'll try to combine Felipe's version (tweaked as per Per's comment)
with jQuery's (to cover any browser cases Felipe's version doesn't)
and post the result to this list.

However, I do have one concern. It seems pretty clear that this
method is going to have to use a certain amount of browser detection
to work (well, browser detection sucks, so I'll try to implement it as
feature detection, but it will amount to the same thing). Currently
the only browser/feature detection I know of in Mochikit is
Mochikit.Signal's hidden _browserAlreadyHasMouseEnterAndLeave function
(which doesn't actually check for either of those events; it just
checks whether the UA string has MSIE in it).

So I COULD just create a few more methods like
_browserAlreadyHasMouseEnterAndLeave to make "ondomready" work, OR I
could add "real" browser detection functionality to Mochikit.
Personally, I think the latter approach is better (it gives Mochikit
more tools, and makes any future browser-dependent coding easier), but
then again Bob et. all might have reasons for wanting to keep that
sort of thing out of Mochikit.

If there is support for "real" browser detect functions (as opposed to
hidden ones that are only used within Mochikit), I'd almost certainly
startwith PPK's browser detector:
http://www.quirksmode.org/js/detect.html

Thoughts?

Jeremy

On May 6, 10:58 pm, "Bob Ippolito" <b...@redivi.com> wrote:
> This would definitely be convenient, it's always been on my list and
> I've hacked together crappy (polling) implementations once or twice,
> but I never needed it bad enough to tackle all of the cross-browser
> issues myself :)
>
> On Tue, May 6, 2008 at 10:51 PM, Per Cederberg <cederb...@gmail.com> wrote:
>
> > I liked the "ondomready" name and structure. Looking at MSDN, they
> > provide the following solution instead of writing new script tags into
> > the document (not tested, just pasted):
>
> > document.onreadystatechange=fnStartInit;
> > function fnStartInit()
> > {
> > if (document.readyState=="complete")
> > {
> > // Finish initialization.
> > }
> > }
>
> > Otherwise I like the proposed solutions and vote for inclusion to
> > MochiKit.Signal. Never know when it might be handy.
>
> > Cheers,
>
> > /Per
>
Reply all
Reply to author
Forward
0 new messages