Web design creating cool web sites with html, xhtml, and css

Page 264

Chapter 11: Activating Your Pages with JavaScript Remember the mathematical expression above? Here’s how it looks in JavaScript:

237

var x, y;

y = 3;

x = y + 4;

That’s the JavaScript equivalent of x = y + 4. Not too hard, is it?

Where do you put JavaScript? Before you delve any further into JavaScript, you’re probably wondering where this stuff goes on your page. The answer is that JavaScript should always live within a <script> block, as shown here: <script language=”javascript”>

var x, y;

y = 3;

x = y + 4;

</script>

This <script> block adds two variables within the JavaScript portion of your Web page, named x and y. The former has the value of 7, and the latter has a value of 3. You can have more than one <script> block on your page, and later <script> can reference variables set and functions defined by earlier blocks.

Events Most people find that tying JavaScript to specific Web page events (quite literally, something that happens), including onLoad and onUnload among others, gives them more than enough flexibility. Table 11-1 shows a list of interesting JavaScript events.

Table 11-1: Interesting Scriptable Events in JavaScript Event Name

Description

onblur

Input element loses focus (user moves cursor elsewhere)

onchange

Similar to oblur, but contents change

onclick

A mouseclick event occurs

ondblclick

A double-click occurs

onfocus

User clicks into, or tabs into, an input element Continued


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.