Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Can script determine if window.onload has already fired?

Reply
Thread Tools

Can script determine if window.onload has already fired?

 
 
Matt Kruse
Guest
Posts: n/a
 
      11-19-2009
There is a discussion going on in the jquery-dev mailing list that
piqued my curiosity. I'm interested to know if the smart people here
have a good answer.

Is there a cross-browser way, in script, to determine if window.onload
has already fired?

If a script is lazy loaded and needs the DOM to be ready, it can
attach to the window's load event. But if that has already fired, then
the script's code will never run. Alternatively, it can just fire its
code inline, but the DOM may not be ready.

Thoughts?

Matt Kruse
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-19-2009
Matt Kruse wrote:

> There is a discussion going on in the jquery-dev mailing list that
> piqued my curiosity. I'm interested to know if the smart people here
> have a good answer.
>
> Is there a cross-browser way, in script, to determine if window.onload
> has already fired?
>
> If a script is lazy loaded and needs the DOM to be ready, it can
> attach to the window's load event.


But it should not. The loading state of the window has nothing to do with
the loading state of the document. We've been over this.

> But if that has already fired, then the script's code will never run.
> Alternatively, it can just fire its code inline, but the DOM may not be
> ready.
>
> Thoughts?


Smart people use the standards-compliant `onload' attribute instead.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$>
 
Reply With Quote
 
 
 
 
Matt Kruse
Guest
Posts: n/a
 
      11-19-2009
On Nov 19, 12:30*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
> But it should not. *The loading state of the window has nothing to do with
> the loading state of the document. *We've been over this.


Fine then, substitute in any other method you prefer.

> Smart people use the standards-compliant `onload' attribute instead.


Could you be more specific?

Matt Kruse

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-19-2009
Matt Kruse wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Smart people use the standards-compliant `onload' attribute instead.

>
> Could you be more specific?


<body onload="...">


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
 
Reply With Quote
 
Matt Kruse
Guest
Posts: n/a
 
      11-19-2009
On Nov 19, 1:06*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
> * <body onload="...">


Script cannot add itself to this attribute, though.

You're steering away from the real question, anyway. Regardless of
whether you feel it should be used, is there any cross-browser way to
detect if window.onload has already fired?

Matt Kruse



 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      11-19-2009
On Nov 19, 3:07*pm, Matt Kruse <m...@thekrusefamily.com> wrote:
> On Nov 19, 1:06*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
> wrote:
>
> > * <body onload="...">

>
> Script cannot add itself to this attribute, though.


Groan. Script doesn't have to "add itself" to this attribute.

>
> You're steering away from the real question, anyway.


Yes.

> Regardless of
> whether you feel it should be used, is there any cross-browser way to
> detect if window.onload has already fired?


Of course. Attach a load listener (or use the standard onload
attribute). They both do the same thing. One is proprietary and one
is not.

The proprietary window.onload property likely came about so that
scripts in the HEAD could set a load listener to the BODY before the
body is ready.

Now, how to know if the BODY load event has fired? Set a flag, of
course.
 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      11-19-2009
On Nov 19, 12:55*pm, Matt Kruse <m...@thekrusefamily.com> wrote:

[...]

>
> If a script is lazy loaded and needs the DOM to be ready, it can
> attach to the window's load event. But if that has already fired, then
> the script's code will never run. Alternatively, it can just fire its
> code inline, but the DOM may not be ready.
>


And what sort of scheme are they working on where their scripts are
unclear about whether the load event has fired? Sounds like a recipe
for disaster to me.

<body onload="loaded = true;">

Or, as mentioned, set window.onload if you think that is a cooler,
more "unobtrusive" solution (despite the inherent drawbacks).
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-19-2009
David Mark wrote:

> On Nov 19, 3:07 pm, Matt Kruse <m...@thekrusefamily.com> wrote:
>> On Nov 19, 1:06 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
>> wrote:
>> > <body onload="...">

>>
>> Script cannot add itself to this attribute, though.

>
> Groan. Script doesn't have to "add itself" to this attribute.


See below.

>> You're steering away from the real question, anyway.

>
> Yes.


No. I was just closing at it at a slower pace

>> Regardless of
>> whether you feel it should be used, is there any cross-browser way to
>> detect if window.onload has already fired?

>
> Of course. Attach a load listener (or use the standard onload
> attribute). They both do the same thing. One is proprietary and one
> is not.


But if we assume that, due to the flawed concept, there are only "lazy-
loaded" scripts, there is a chicken-and-the-egg problem with adding a load
listener with scripting. Hence my recommendation to use the `onload'
attribute of the `body' element in the first place.

> The proprietary window.onload property likely came about so that
> scripts in the HEAD could set a load listener to the BODY before the
> body is ready.


Unlikely: <http://docs.sun.com/source/816-6408-10/handlers.htm#1120545>

> Now, how to know if the BODY load event has fired? Set a flag, of
> course.


That is what I was aiming at. But some people apparently need it to be
spelt to them, so thank you for that.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      11-19-2009
On Nov 19, 5:34*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
> David Mark wrote:
> > On Nov 19, 3:07 pm, Matt Kruse <m...@thekrusefamily.com> wrote:
> >> On Nov 19, 1:06 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
> >> wrote:
> >> > <body onload="...">

>
> >> Script cannot add itself to this attribute, though.

>
> > Groan. *Script doesn't have to "add itself" to this attribute.

>
> See below.
>
> >> You're steering away from the real question, anyway.

>
> > Yes.

>
> No. *I was just closing at it at a slower pace


I figured. I'd just as soon be done with the discussion.

>
> >> Regardless of
> >> whether you feel it should be used, is there any cross-browser way to
> >> detect if window.onload has already fired?

>
> > Of course. *Attach a load listener (or use the standard onload
> > attribute). *They both do the same thing. *One is proprietary and one
> > is not.

>
> But if we assume that, due to the flawed concept, there are only "lazy-
> loaded" scripts, there is a chicken-and-the-egg problem with adding a load
> listener with scripting. *Hence my recommendation to use the `onload'
> attribute of the `body' element in the first place.


Right. The whole question is silly.

>
> > The proprietary window.onload property likely came about so that
> > scripts in the HEAD could set a load listener to the BODY before the
> > body is ready.

>
> Unlikely: <http://docs.sun.com/source/816-6408-10/handlers.htm#1120545>


I don't follow.
 
Reply With Quote
 
Jorge
Guest
Posts: n/a
 
      11-19-2009
On Nov 19, 11:34*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
>
> Unlikely: <http://docs.sun.com/source/816-6408-10/handlers.htm#1120545>


If you specify an onLoad event handler for an Image object that
displays a looping GIF animation (multi-image GIF), each loop of the
animation triggers the onLoad event, and the event handler executes
once for each loop.

Is this true ?
--
Jorge.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Block execution of script while same script is already running MrBanabas@googlemail.com Ruby 2 12-17-2008 06:03 PM
Can a script determine its own location? Martin Larsen Javascript 5 05-22-2007 11:35 AM
How to determine whether the user has already selected an option from a multi select list Patrick Olurotimi Ige ASP .Net 6 03-11-2005 10:39 AM
nuby: determine method passed and determine the receiver that received the method Peņa, Botp Ruby 1 01-24-2004 07:51 PM
How to determine if an instance of your program is already running? Mike Python 5 12-28-2003 08:26 AM



Advertisments