472,958 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 4481
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Stuart | last post by:
I am very new to javaScript, and I am hoping someone can explain the differance between the following two browsers and how to deal with thier differant approach to firing the onLoad event. The...
12
by: HarveyB | last post by:
I would like to generate non-modal popup windows from ASP.Net code-behind. I have tried using Client Side scripting like "function Test(){ window.open('test.htm',_blank,...
5
by: Jeff Thies | last post by:
I have this IE specific bit of code for finding the originating node: var obj=window.event.srcElement; How do I do that cross browser (Opera, NS, Safari...)? Is there a standard DOM method? ...
4
by: jadiyo | last post by:
Hi, I've read many questions about how to automatically open a new window from a page using the JavaScript onLoad command. Unfortunately, due to the web application framework being used...
6
by: Luke Matuszewski | last post by:
Why window.onclick isn't fired in IE and is fired on Gecko agents ? <html onclick="alert('Event is now at the HTML element.')"> <head> <title>Event Bubbles</title> <script...
22
by: stephen | last post by:
I have created an order form that users javascript to create a new html document when the customers clicks the "print page" button. Once the new document has been created it then prints the...
7
by: Andrew Poulos | last post by:
In the head of my page I have: <script type="text/javascript" src="scripts/test.js"></script> and later in the head I have: <script type="text/javascript"> window.onload = function() { foo...
4
by: Jason | last post by:
Hi, Here's the scenario: I have a web application that has window A and window B. A user has both window A and B open - window A is in the foreground and window B is behind it. If the...
20
by: Mark Anderson | last post by:
Hi, I have this in an external JS library: ///////////////////////// function addMyEvent(){ var obj; if(document.attachEvent) { obj = document.getElementsByTagName('img'); for...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.