04.07.2013 Views

Building iPhone Apps with HTML, CSS, and ... - Cdn.oreilly.com

Building iPhone Apps with HTML, CSS, and ... - Cdn.oreilly.com

Building iPhone Apps with HTML, CSS, and ... - Cdn.oreilly.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

• Array keys are assigned beginning at 0.<br />

• The single equals sign is the assignment operator.<br />

• The double equals sign is the equivalence logical operator.<br />

• The plus sign is the string concatenation operator.<br />

For our purposes, the most important feature of JavaScript is that it can interact <strong>with</strong><br />

the elements of an <strong>HTML</strong> page (the cool kids call this “manipulating the DOM”).<br />

Example 1-9 shows a simple bit of JavaScript that changes some text on the page when<br />

the user clicks on the h1.<br />

DOM st<strong>and</strong>s for Document Object Model, <strong>and</strong> in this context it represents<br />

the browser’s underst<strong>and</strong>ing of an <strong>HTML</strong> page. You can read more<br />

about the Document Object Model here: http://en.wikipedia.org/wiki/<br />

Document_Object_Model.<br />

Example 1-9. Simple OnClick h<strong>and</strong>ler<br />

<br />

<br />

My Awesome Page<br />

<br />

function sayHello() {<br />

document.getElementById('foo').inner<strong>HTML</strong> = 'Hi there!';<br />

}<br />

<br />

<br />

<br />

Click me!<br />

<br />

<br />

Here’s an explanation:<br />

I’ve added a script block to the head of the <strong>HTML</strong> document.<br />

Inside the script block, I’ve defined a single JavaScript function named sayHello().<br />

The sayHello() function contains a single statement, which tells the browser to “look<br />

through the document for an element that has the id ‘foo’, <strong>and</strong> set its inner<strong>HTML</strong><br />

contents to ‘Hi there!.’” The effect of this in the browser is that the text “Click me!”<br />

will be replaced <strong>with</strong> “Hi there!” when the user clicks on the h1 element.<br />

End of the sayHello() function.<br />

End of the script block.<br />

The onclick attribute of the h1 element tells the browser to do something when the<br />

user clicks on the h1, namely, to run the sayHello() function.<br />

10 | Chapter 1: Getting Started

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!