HTML5, CSS3, and JavaScript Fourth Edition

(Ben Green) #1

Chapter 36


ch32 Untangling HTML and


Scripting


Contents


36.1 Add Event Listener................. 294
36.2 ezCalc......................... 294

You will have noticed that in our examples, we placed “onmouseover” di-
rectly into the HTML of the webpage.


This is called in-line scripting.


In-line scripting is not best because it mixes HTML and scripting together.
In an ideal world, we would put as little JavaScript into the HTML as
possible. That is because mixing makes things more complicated.


Why untangle? (We said this before in the CSS unit, but it bears repeating.)
Writing content (HTML) is a skill. Styling content (CSS) is a skill. Scripting
active content (JavaScript) is a skill. It is easier to find three persons who
enjoy and are each expert in one area than it is to find one person who
enjoys and is expert in all three areas.


We run into a similar problem when we usestyle="..."directly in the
HTML markup. It is better to separate it out and keep it in a style sheet.


But how can we keep the JavaScript separate from the HTML?


293

Free download pdf