So I've been going through Head First JavaScript and I came to a section on Event Handling with Function Literals. The book explains that you can wire all your event handling in your 'script' tags. But I am confused on how I get multiple functions to fire off on one event. Here's my code:
//Event Handling with Function Literals
window.onload = function(evt) {
//THIS IS BROKEN
document.body.onresize = resizeImg();reportImgHeight();
//Onload: Functions to Execute -- THESE WORK
resizeImg();
reportImgHeight();
}
So specifically for this example, how do I get an "onresize" event to execute BOTH resizeImg and reportImgHeight (functions which I have defined elsewhere in my code). Thank you!
Denys Séguret :
The cleanest solution is to use addEventListener :\n\nwindow.addEventListener('resize', resizeImg);\nwindow.addEventListener('resize', reportImgHeight);\n\n\nThis way you can decouple both bindings. \n\nNote also that you should bind the resize event to the window, not to a document part.",
2013-08-25T13:50:33