May 2017
Intermediate to advanced
448 pages
10h 10m
English
The traditional mechanism for registering event handlers through JavaScript (rather than adding handler attributes right in the HTML content) is to assign a function to the DOM element's corresponding property. For example, suppose we had defined the following function:
function doStuff() { // Perform a task... }
We could then either assign it within our HTML markup:
<body onload="doStuff();">
Or, we could assign it from within JavaScript code:
window.onload = doStuff;
Both of these approaches will cause the function to execute when the page is loaded. The advantage of the second is that the behavior is cleanly separated from the markup.
Read now
Unlock full access