Web Design with HTML and CSS

(National Geographic (Little) Kids) #1

220


Interactivity on the web

Web Design with HTML and CSS Digital Classroom

4 After changing the event to onload, save your page and preview it in your browser. As
soon as your page opens, you trigger the prompt window. You could enter the text here,
but as the event is currently structured, it would write the text to the page, so click
Cancel.

The onload event can be useful, but for this example, it would be distracting for the user.

With this exercise, you have learned that JavaScript lets you choose where and how you call
it. In both cases, user interaction triggers the code, but the onload event gives the user little
choice as to when to trigger the code, whereas the onclick event (attached to the button),
gives the user more choice.

Placing your JavaScript into an external document


You can save JavaScript in an external fi le that is linked from your HTML pages in much the
same way you do with external style sheets. The benefi ts are the same: to easily update code
that’s located in a single fi le.

1 In your text editor, choose File > New and then choose File > Save. In the dialog
box that appears, save this fi le in your web10lessons folder as promptwindow.js. The
extension .js is for external JavaScript fi les.
Free download pdf