views:

247

answers:

3

hi,

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

+2  A: 

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

window.addEventListener ("load",initEventHandlers,false);
J-P
this will not work because the DOM had not been loaded yet, so he can't use document.getElementById()...
Marwan Aouida
The DOM does not need to be loaded in order to add an event to the window object!
J-P
I knowbut 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
Marwan Aouida
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...
J-P
@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...
Christoph
+1  A: 

What about:

window.onload = initEventHandlers;

This will work for you.

José Leal
A: 

Hi Gayell,

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!

Lewis
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?
roryf
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().
cletus
Events in javascript are fundamental. The cross browser problems aren't that complicated either. Perhaps we should all use GWT and forget javascript altogether?
meouw
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.
Lewis
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!
Lewis