472,989 Members | 3,031 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,989 software developers and data experts.

How can I be notified when the document load is complet in JavaScript

Hi,

How can I be notified when the document load is complet in JavaScript?
I am referring to the whold document load is complete, mean all
images/external files/frame/iframes have been loaded.

I think there is a window.addEventListener() function but I am not sure
which event to listen to.

Thank you.

Dec 18 '05 #1
6 2314
VK

sy*******@gmail.com wrote:
Hi,

How can I be notified when the document load is complet in JavaScript?
I am referring to the whold document load is complete, mean all
images/external files/frame/iframes have been loaded.

I think there is a window.addEventListener() function but I am not sure
which event to listen to.

Thank you.


1. Grey-old way:
<body onload="myFunction()">
Note that this is a real function call so parenthesis are obligatory;
also you can provide some arguments to your function:
<body onload="myFunction('Hello world!')">

2. Up to date the most used one:
<script type="text/javascript">
finction myFunction() {
// ...
}

window.onload = myFunction;
</script>
Note that this is a function *reference* so no parenthesis are allowed;
also you cannot directly provide arguments to your function.

3. High-teched one ;-)
<script type="text/javascript">
finction myFunction() {
// ...
}

if (window.addEventListener) {
// using dis-on'ed event name:
window.addEventListener('load', myFunction, false);
}
else if (window.attachEvent) {
// using on'ed event name:
window.attachEvent('onload', myFunction);
}
else {
// some old crap.
// either use option 2 from above
// or
// alert('Get yourselve a normal browser!');
}
</script>

Also please not that "window.load" event doesn't mean that every single
bit of page is loaded. It can be enbedded "Star Wars" movie on the
page, so the actual loading will be finished an hour or two later.
"window.load" means that DOM structure is fully parsed so you can
reliably address any DOM element on the page by id / name, change src
and other attributes and so on - briefly you are able to script the
loaded page.

Dec 18 '05 #2
VK wrote:
sy*******@gmail.com wrote:
How can I be notified when the document load is complet in JavaScript?
I am referring to the whold document load is complete, mean all
images/external files/frame/iframes have been loaded.

I think there is a window.addEventListener() function but I am not sure
which event to listen to.
[...]
1. Grey-old way:


Nonsense. It is the standards compliant way and the more recent one:
<body onload="myFunction()">
Note that this is a real function call so parenthesis are obligatory;
also you can provide some arguments to your function:
<body onload="myFunction('Hello world!')">

2. Up to date
Nonsense. It is the other way around. `window.onload' has been there in
AOMs before HTML 4.01 where the `onload' attribute and other event handler
attributes were introduced.
the most used one:
Which does not make it in any way more correct. Especially when such a
statement comes from you who has proven not to understand what he is doing.
<script type="text/javascript">
finction myFunction() {
function ...
// ...
}

window.onload = myFunction;
This is the proprietary approach, dating back to IE3/NN3. It also does not
ensure that the document was loaded; it ensures only that the window was
loaded, iff the object referred to by `window' supports an `onload' event
handler property.
</script>
Note that this is a function *reference* so no parenthesis are allowed;
Nonsense. Parantheses, i.e. the Call Operator, are very well allowed here,
but that changed the meaning of that AssignmentExpression as the return
value of the called function would be used then. It is entirely possible
that a function call may return a Function reference.
also you cannot directly provide arguments to your function.
True, however you can indirectly:

window.onload = function(e)
{
myFunction('foo');
}
3. High-teched one ;-)
Nonsense. The first branch is the standards compliant approach to
implement the feature the standards compliant attribute of the `body'
element facilitates, using scripting only:
<script type="text/javascript">
finction myFunction() {
It is still `function'.
// ...
}

if (window.addEventListener) {
// using dis-on'ed event name:
window.addEventListener('load', myFunction, false);
addEventListener() is not a method of Window objects, but of EventTarget
objects, according to the W3C DOM Level 2 Events Specification. The
`load' event there does not apply to Window objects in HTML context,
but to the document, frameset and object element, hence HTMLDocument,
HTMLFramesetElement and HTMLObjectElement objects, where the first one
is usually implemented referred to by the `document' property.

<URL:http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventTarget>
<URL:http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-eventgroupings-htmlevents>

And of course the feature detection used here is flawed. So it should be
something more like

function isMethodType(s)
{
return (s == "function" || s == "object");
}

var d;
if (typeof window != "undefined"
&& typeof (d = window.document) != "undefined"
&& isMethodType(typeof d.addEventListener))
{
d.addEventListener('load', myFunction, false);
}
}
else if (window.attachEvent) {
// using on'ed event name:
The main difference is that the former, if it was used correctly, is the
standards compliant approach while attachEvent() is the IE-proprietary one:
window.attachEvent('onload', myFunction);
}
else {
// some old crap.
// either use option 2 from above
// or
// alert('Get yourselve a normal browser!');
More of the usual VK nonsense.
}
</script>

Also please not that "window.load" event doesn't mean that every single
bit of page is loaded. It can be enbedded "Star Wars" movie on the
page, so the actual loading will be finished an hour or two later.
"window.load" means that DOM structure is fully parsed
No, that is exactly what it does not mean.
so you can reliably address any DOM element on the page by id / name,
change src and other attributes and so on - briefly you are able to script
the loaded page.


No, you cannot and you are not reliably then.

Your advice is considered harmful rightfully. Stop pretending
you had a minimum clue about the matters discussed here.
PointedEars
Dec 18 '05 #3
VK
To OP (Original Poster):

Thomas 'PointedEars' Lahn is a local disaster of this *unmoderated*
newgroup.

To see his regular style, informativeness and attitude of the real
newsgroup gurus towards him (I am not one - just another victime) you
may look at recent threads like:
<http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/01dd76b30c37d44c/611fc00508c5dc47#611fc00508c5dc47>

I hope my aswer helped you. If any more questions please do not
hesitate *and do not be scared* to ask.

Dec 18 '05 #4
VK wrote:
To OP (Original Poster):
Private e-mail exists.
Thomas 'PointedEars' Lahn is a local disaster of this *unmoderated*
newgroup.


In a moderated newsgroup, you would hopefully not be allowed to post such
nonsense as you did and repeatedly do, hence it would not be necessary to
correct you.
PointedEars
Dec 18 '05 #5
Thomas 'PointedEars' Lahn said the following on 12/18/2005 12:20 PM:
VK wrote:

To OP (Original Poster):

Private e-mail exists.


This is Usenet PointedHead, learn that. You ask here, you get answered
here.

Thomas 'PointedEars' Lahn is a local disaster of this *unmoderated*
newgroup.

In a moderated newsgroup, you would hopefully not be allowed to post such
nonsense as you did and repeatedly do, hence it would not be necessary to
correct you.


And the same goes for you Thomas.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 18 '05 #6
On 2005-12-18, sy*******@gmail.com <sy*******@gmail.com> wrote:
Hi,

How can I be notified when the document load is complet in JavaScript?
I am referring to the whold document load is complete, mean all
images/external files/frame/iframes have been loaded.

I think there is a window.addEventListener() function but I am not sure
which event to listen to.

Thank you.


listen to all the onloads, when they're all done your page is loaded.

something _like_ this.

<html><head>
<script type="text/javascript">

var items_to_load=4; // this many items to load

function loaded(){
if (--items_to_load) return;
alert("page is completely loaded"); // do this when all is loaded
}

</script>
</head><body onload="loaded()">
<img onload="loaded()" src="gif1.gif">
<img onload="loaded()" src="gif2.gif">
<img onload="loaded()" src="gif3.gif">
</body></html>



--

Bye.
Jasen
Dec 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mat | last post by:
How can I detect when a link has been clicked but the new page is still in the process of loading? The document.location.href property still displays the current location (understandably) not the...
3
by: Catherine Lynn Smith | last post by:
I am creating a webpage with dhtml <DIV> layers and I want a link on one layer to modify the content on another but I seem to keep running into errors. Basically I create a layer in the middle...
2
by: Halldór Ísak Gylfason | last post by:
In my application I have an iframe that is empty (and not visible) initially, however when a user presses a button a form is programmatically submitted and the target is set to the IFrame. I...
2
by: Richard Bell | last post by:
Newbie question ... please respond to group I'm trying to open/navigate to a url. How can I tell when the page is fully loaded? Testing suggest that window.open returns before the page is...
4
by: Jake Lewis | last post by:
I have an HTML page that loads fine including the .js file <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Untitled Document</title> <script...
7
by: Remi Bastide | last post by:
I'm trying to open a blank window and write a message in it. The following page works as expected in IE, but in Firefox the message is not written: <HTML> <HEAD> <TITLE>Document.write...
3
by: Stevie_mac | last post by:
It might be me but... I dont seem to get a Page_Load event when a opening an ASPX in an iFrame. I do geta Page_Load event when an item on the ASPX (inside the iFrame) is clicked but then...
13
by: a1eah71 | last post by:
I have an application where the user loads graphics from another site. Presently I advise the user to wait for graphics to load before clicking on adjustments. Ideally, I would prefer a closed-loop...
5
by: HopfZ | last post by:
I made two shortcut functions for document.getElementById as: function EBI2(id){return document.getElementById(id)}; var EBI3 = document.getElementById; But EBI3 don't work. EBI2('myText');...
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...
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...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
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...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.