471,316 Members | 1,491 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,316 software developers and data experts.

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

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
Jul 23 '05 #1
6 4339
"Brian" <ta********@cliaskioja.mailexpire.com> wrote in message
news:b7**************************@posting.google.c 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
Jul 23 '05 #2

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!
Jul 23 '05 #3
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
Jul 23 '05 #4
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 <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #5
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 <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #6
Lee
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>

Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Stuart | last post: by
12 posts views Thread by HarveyB | last post: by
5 posts views Thread by Jeff Thies | last post: by
6 posts views Thread by Luke Matuszewski | last post: by
7 posts views Thread by Andrew Poulos | last post: by
20 posts views Thread by Mark Anderson | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.