15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

486<br />

The Structure of a JavaScript Program<br />

Despite its name, JavaScript is a completely different <strong>programming</strong> language<br />

from Java, although both languages borrow heavily from the C++ syntax that<br />

includes the use of curly brackets and semicolons ending each statement.<br />

On the Mac OS X, Dashboard widgets, which are tiny programs that pop up<br />

on the screen, are made with JavaScript code.<br />

The Structure of a JavaScript Program<br />

At the simplest level, a JavaScript program can consist of one or more commands,<br />

such as<br />

<br />

<br />

document.writeln(“This is a simple JavaScript program.”);<br />

<br />

<br />

<br />

<br />

The and tags define the beginning and ending of a<br />

JavaScript program. The first tag identifies the scripting language<br />

used, which in this case is always JavaScript. Sandwiched in between the<br />

two tags are the actual JavaScript commands.<br />

However, it’s more common to divide a JavaScript program into functions<br />

with each function acting like a separate building block. For example, a<br />

simple JavaScript program might look like this:<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

The onLoad command tells your computer when to run a particular function.<br />

As an alternative to storing JavaScript code directly in the HTML code of a<br />

Web page, you can store JavaScript programs in separate files. Then you

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

Saved successfully!

Ooh no, something went wrong!