I am looking for the most proper and efficient way to bind Javascript events; particularly the onload
event (I would like the event to occur after both the page AND all elements such as images are loaded). I know there are simple ways to do this in jQuery but I would like the more efficient raw Javascript method.

- 5,753
- 72
- 57
- 129

- 183
- 1
- 1
- 4
4 Answers
There are two different ways to do it. Only one will work; which one depends on the browser. Here's a utility method that uses both:
function bindEvent(element, type, handler) {
if(element.addEventListener) {
element.addEventListener(type, handler, false);
} else {
element.attachEvent('on'+type, handler);
}
}
In your case:
bindEvent(window, 'load', function() {
// all elements such as images are loaded here
});

- 128,221
- 31
- 203
- 222
-
Thank you very much. Very thorough and exactly what I'm looking for! – Jackie Nov 25 '09 at 11:03
-
@Jackie: the fun doesn't end here. See my answer. – Crescent Fresh Nov 25 '09 at 13:42
-
The event itself is a dependency that can be avoided in large part by understanding DOM. DOM loads in a top-to-bottom fashion; items nearer the top of the document load earlier while items toward the end later. By loading scripts that have DOM dependencies at the end of your document many of these concerns can be mitigated with no event binding at all. Keep it simple. – augurone Oct 08 '14 at 17:04
-
@KingRider: Which browser and version? – David Hedlund Jun 29 '16 at 21:11
-
@DavidHedlund newest version with win 10 to Internet Explorer – KingRider Jun 30 '16 at 02:48
-
@KingRider: Internet Explorer 11 doesn't have `element.attachEvent` but it has `element.addEventListener` so if your code is exactly as above, it shouldn't hit that `else` clause. Verify that `element` is indeed a html node (and not, say, a jQuery wrapped element, but then I guess you're not using jQuery if you're using this code at all) – David Hedlund Jun 30 '16 at 06:08
-
@DavidHedlund not used jquery, but need jquery? topic tag just javascript =S .. i know jquery ´.bind()` ... i need javascript pure – KingRider Jun 30 '16 at 14:48
-
Yeah, you shouldn't need jQuery. IE 11 has `addEventListener`. – David Hedlund Jun 30 '16 at 15:10
I know you did only ask about how to bind events. But Ooooo boy the fun doesn't end there. There's a lot more to getting this right cross-browser than just the initial binding.
@d.'s answer will suffice just fine for the specific case of the load
event of window
you're looking for. But it may give novice readers of your code a false sense of "getting it right". Just because you bound the event doesn't mean you took care to normalize it. You may be better of just fixing window.onload
:
window.onload = (function(onload) {
return function(event) {
onload && onload(event);
// now do your thing here
}
}(window.onload))
But for the general case of event binding @d.'s answer is so far from satisfying as to be frustrating. About the only thing it does right is use feature-detection as opposed to browser detection.
Not to go on too much of a rant here but JavaScript event binding is probably the #1 reason to go with a JavaScript library. I don't care which one it is, other people have fixed this problem over and over and over again. Here're the issues in a home-brewed implementation once inside your handler function:
- How do I get a hold of the
event
object itself? - How do I prevent the default action of the event (eg clicking on a link but not navigating)
- Why does
this
point to thewindow
object all the time? - (Re mouse events) What are the x/y coords of the event?
- Which mouse button triggered the event?
- Was it a Ctrl-click or just a regular click?
- What element was the event actually triggered on?
- What element is the event going to? (ie
relatedTarget
, say forblur
) - How do I cancel the bubbling of the event up through its parent DOM?
- (Re event bubbling) what element is actually receiving the event now? (ie
currentTarget
) - Why can't I get the freaking char code from this
keydown
event? - Why is my page leaking memory when I add all these event handlers?? Argh!
- Why can't I Unbind this anonymous function I bound earlier?
And the list goes on...
The only good reason to roll your own in these days is for learning. And that's fine. Still, read PPK's Intro to browser events. Look at the jQuery source. Look at the Prototype source. You won't regret it.

- 115,249
- 25
- 154
- 140
-
There are instances where loading a **library** or **framework** are impractical and are overkill for any number of reasons (early vs late loading, bloat of js for "simple functionality", ...). – augurone Oct 23 '14 at 18:33
Something like that
window.onload = (function(onload) {
return function(event) {
onload && onload(event);
$(".loading-overlay .spinner").fadeOut(300),
$(".loading-overlay").fadeOut(300);
$("body").css({
overflow: "auto",
height: "auto",
position: "relative"
})
}
}(window.onload));

- 11,397
- 1
- 26
- 37