473,416 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 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 4524
"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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.