Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How to tell if the window.onload event has already fired?

Reply
Thread Tools

How to tell if the window.onload event has already fired?

 
 
Brian
Guest
Posts: n/a
 
      07-07-2004
Hi everyone,

I'm writing a function (in javascript) that needs to do one thing if
the page has not loaded, and another (different) thing if the page has
already loaded.

I'm looking for a way to tell if the window.onload event has already
fired. I cannot edit the onload event handler itself, and my function
can only exist in an external js file, sourced from the document's
head section. Any ideas?

Thanks,
Brian
 
Reply With Quote
 
 
 
 
Blue Raja
Guest
Posts: n/a
 
      07-08-2004
"Brian" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Hi everyone,
>
> I'm writing a function (in javascript) that needs to do one thing if
> the page has not loaded, and another (different) thing if the page has
> already loaded.
>
> I'm looking for a way to tell if the window.onload event has already
> fired. I cannot edit the onload event handler itself, and my function
> can only exist in an external js file, sourced from the document's
> head section. Any ideas?


How about:

src.js
[STARTFILE]
var hasLoaded = false;

function myfunc(/* param list */){
if (hasLoaded){
// do post-load stuff here
} else {
// do pre-load stuff here
}
}
[ENDFILE]

file.html
[STARTFILE]
<html>
<!-- head declaration -->
<body onload="hasLoaded = true;">
<!-- body stuff -->
</body>
</html>
[ENDFILE]

Hope that helps.

--

Jason, aka The Blue Raja


 
Reply With Quote
 
 
 
 
Brian K
Guest
Posts: n/a
 
      07-08-2004

Thanks, but as I said, I cannot edit the onload event handler itself...



*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Mick White
Guest
Posts: n/a
 
      07-08-2004
Brian K wrote:
> Thanks, but as I said, I cannot edit the onload event handler itself...
>
>
>
> *** Sent via Devdex http://www.devdex.com ***
> Don't just participate in USENET...get rewarded for it!



....
window.onload=myFunc
</script>
</head>
<body>
....


Mick
 
Reply With Quote
 
Grant Wagner
Guest
Posts: n/a
 
      07-08-2004
Mick White wrote:

> Brian K wrote:
> > Thanks, but as I said, I cannot edit the onload event handler itself...

> ...
> window.onload=myFunc
> </script>
> </head>
> <body>
> ...


And if the onload event is already defined on the page, that line, if placed
in the <head>, will be replaced by the onload event defined in the <body>. If
placed in the <body>, it will replace any existing onload event for the page.

Injecting code for execution onload when you have no other control over the
page is problematic at best. The following works in IE6SP1, Firefox 0.9.1,
Mozilla 1.7, Opera 7.5.2 and Netscape 4.78 on an x86 under Windows XP. I make
no claims about other browsers on other platforms.

<head>
<script type="text/javascript">
function appendOnloadEvent(eventHandler) {
if (window.onload) {
// there is currently an onload event defined for
// window; append the event handler javascript to
// the current event handler javascript
window.onload = new Function(
// take the original event
window.onload
// convert it to a string; the string looks like:
// "...function...()...{...[js]...}..."
.toString()
// remove any newlines or carriage returns
.replace(/[\r\n]/g, '')
// remove any leading or trailing whitespace
.replace(/\s+$|^\s+/g, '')
// turn "function...()...{...[js]...}"
// into "function...()...{...[js][newline][new js]...}"
.replace(
/^function\s*\w+\s*\(\w*\)\s*\{\s*(.*)\s*\}$/,
'$1\n' + eventHandler
)
);
} else {
// there is currently no onload event defined for this
// window; set the event handler javascript to handle
// the event
window.onload = new Function(eventHandler);
}
}

function test() {
alert('bye');
}
</script>
</head>

<body onload="alert('hi');">
<script type="text/javascript">appendOnloadEvent('test();');</script>
</body>

But even it requires script injection inside the <body></body> to work
properly.

--
Grant Wagner <(E-Mail Removed)>
comp.lang.javascript FAQ - http://jibbering.com/faq


 
Reply With Quote
 
Mick White
Guest
Posts: n/a
 
      07-08-2004
Grant Wagner wrote:

> Mick White wrote:
>
>
>>Brian K wrote:
>>
>>>Thanks, but as I said, I cannot edit the onload event handler itself...

>>
>>...
>> window.onload=myFunc
>></script>
>></head>
>><body>
>>...

>
>
> And if the onload event is already defined on the page, that line, if placed
> in the <head>, will be replaced by the onload event defined in the <body>. If
> placed in the <body>, it will replace any existing onload event for the page.
>

I assumed that that the onload event was not pre-assigned.
See my comments below.


> Injecting code for execution onload when you have no other control over the
> page is problematic at best. The following works in IE6SP1, Firefox 0.9.1,
> Mozilla 1.7, Opera 7.5.2 and Netscape 4.78 on an x86 under Windows XP. I make
> no claims about other browsers on other platforms.
>
> <head>
> <script type="text/javascript">
> function appendOnloadEvent(eventHandler) {
> if (window.onload) {
> // there is currently an onload event defined for
> // window; append the event handler javascript to
> // the current event handler javascript
> window.onload = new Function(
> // take the original event
> window.onload
> // convert it to a string; the string looks like:
> // "...function...()...{...[js]...}..."
> .toString()
> // remove any newlines or carriage returns
> .replace(/[\r\n]/g, '')
> // remove any leading or trailing whitespace
> .replace(/\s+$|^\s+/g, '')
> // turn "function...()...{...[js]...}"
> // into "function...()...{...[js][newline][new js]...}"
> .replace(
> /^function\s*\w+\s*\(\w*\)\s*\{\s*(.*)\s*\}$/,
> '$1\n' + eventHandler


Grant, this is remarkable, a novel approach, indeed.

> )
> );
> } else {
> // there is currently no onload event defined for this
> // window; set the event handler javascript to handle
> // the event
> window.onload = new Function(eventHandler);
> }
> }
>
> function test() {
> alert('bye');
> }
> </script>
> </head>
>
> <body onload="alert('hi');">
> <script type="text/javascript">appendOnloadEvent('test();');</script>
> </body>
>
> But even it requires script injection inside the <body></body> to work
> properly.
>
> --
> Grant Wagner <(E-Mail Removed)>
> comp.lang.javascript FAQ - http://jibbering.com/faq
>
>

 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      07-08-2004
Brian said:
>
>Hi everyone,
>
>I'm writing a function (in javascript) that needs to do one thing if
>the page has not loaded, and another (different) thing if the page has
>already loaded.
>
>I'm looking for a way to tell if the window.onload event has already
>fired. I cannot edit the onload event handler itself, and my function
>can only exist in an external js file, sourced from the document's
>head section. Any ideas?


This seems to work in IE66, Netscape 7.1 and family, and Opera 7.51,
either in the head or in an external .js file.
The Mozillas execute the new handler first, the others execute
it after the original handler.

<html>
<head>
<script type="text/javascript">
function myHandler(){alert("Loaded")}
if(window.addEventListener){
alert("window.addEventListener");
window.addEventListener("load",myHandler,false);
}else if(window.attachEvent){
alert("window.attachEvent");
window.attachEvent("onload",myHandler);
}else{
alert("None of the above");
}
</script>
</head>
<body onload="alert('original onload handler')">
<script type="text/javascript">alert("loading")</script>
done
</body>
</html>

 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
how to check whether an event has been already handled in IE ? wolverine Javascript 1 08-23-2007 09:36 AM
how to check whether an event has been already handled in IE ? wolverine Javascript 0 08-23-2007 05:48 AM
How to tell if PDM is already installed on a PIX? fred.branham@gmail.com Cisco 3 12-06-2005 07:27 PM
In what event aspx to load user control so variable already has value? Merkisson Jourhanssen ASP .Net 0 04-21-2004 04:52 PM



Advertisments