HTML and CSS Reference
event attributes of an HTML element
For years, it has been common to write HTML pages with input fields and buttons explicitly attached
<input type="button" value="Click me" onclick="handleClick()" />
From a purely functional perspective, there's nothing wrong with this code—it works as expected:
function then runs whenever a user clicks the button. This approach, however, is largely acceptable
HTML without including inline style information. Next, designers style elements using CSS classes.
What about the code that is referenced by event handlers? In other words, where would you get
the code for functions like handleClick ?
As browsers encounter one of these <script> sections, they stop page rendering, execute the
script, and then proceed. If the script element doesn't contain immediate code to execute (for
example, suppose it contains only function declarations), then the browser simply takes note of the
function and proceeds. A script element is therefore an acceptable place to embed the definition of
functions invoked by event handlers.
Embedding script code in a page has both pros and cons, but mostly cons. Table 4-3 summarizes both.