2

I try to put an event on document loading but not works... I'd put an alert box, but never seen it...

document.addEventListener ("load",initEventHandlers,false);

function initEventHandlers ()
{
document.getElementbyId ('croixzoom').addEventListener ('click',fermezoom,false);
alert ("Hello, i\'m a eventHAndlers")   
}
function fermezoom (){
document.getElementbyId ('zoom').style.visibility = 'hidden';
document.getElementbyId ('fondzoom').style.visibility = 'hidden';
}

Thanks for you help

3 Answers 3

6

The document does not have an onload / load event, try attaching it to 'window':

window.addEventListener ("load",initEventHandlers,false);
Sign up to request clarification or add additional context in comments.

5 Comments

this will not work because the DOM had not been loaded yet, so he can't use document.getElementById()...
The DOM does not need to be loaded in order to add an event to the window object!
I know but in the code that Gayell wrote he accessed the DOM from the event handler, i think in this case the code will not work if the event hadler is fired for the load event of the window object
But you don't know where he has placed the script tag! If it's placed at the bottom of <body> then the function will work fine...
@Marwan: You're wrong: window.onload fires after the DOM has been built and external resources been loaded - that's the reason why there's DOMContentLoaded and all these onready() hacks: people don't want to wait for all images to be loaded before being able to modify the DOM...
1

What about:

window.onload = initEventHandlers;

This will work for you.

Comments

-1

Updated As a recomendation, not a direct solution to your problem: You might want to consider using a framework if you haven't already. Maybe look at jQuery which should take a lot of the pain out of what you are trying to do.

Then you could wrap it up in syntax like:

 $(document).ready(function() {
   // put all your jQuery goodness in here.
 });

You might not want the additional layer on top of Javascript though.

I hope that helps!

5 Comments

Ah yes, the answer to everything 'use a framework'. That might help productivity but it won't help him learn how events work in Javascript will it?
learning how events work in javascript isn't all that useful (especially the cross browser differences) compared to a lightweight framework that does that for you. but i gotta mark this down: people either forget, ignore or don't know theres a difference between ready() and load().
Events in javascript are fundamental. The cross browser problems aren't that complicated either. Perhaps we should all use GWT and forget javascript altogether?
Rory, fair enough it won't teach much about events in Javascript. Thats why I said 'You might want to consider using a framework' It's always good to get chastised for trying to be helpful I find.
Cletus, You're right I wasn't talking about document.onload ... but he's trying to use document.getElementbyId - which I unless he's injecting content would imply he wants manipulate the DOM. I was simply offering a potential alterntive to how he's doing things. It hardly warrants a mark down!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.