JavaScript is a platform-independent, object-oriented scripting language designed for the web, originally created by Netscape Communications and Sun Microsystems. It was designed to add interactivity to web sites and has since grown into a fundamental tool for web content developers. JavaScript programs—called scripts—are usually embedded in HTML. Features like dynamic typing and event handling, and its interface with a web page’s Document Object Model (DOM), all make JavaScript a very useful extension to HTML.
About JavaScript
About the Document Object Model (DOM)
JavaScript is not a compiled language; rather, it is interpreted during the parsing of an HTML page by a web client—it is not interpreted on the server side. Despite their similar names, JavaScript has no functional equivalence to the Java language; however, technologies like LiveConnect create interoperability between the two.
Scripts can be placed anywhere within an HTML file, but most commonly are placed in the <HEAD>
section, where the page’s title and stylesheet definitions usually reside:
<HEAD> |
<TITLE>My Page</TITLE> |
<SCRIPT LANGUAGE="JavaScript"><!-- |
function myFunction() { |
... |
} |
--> |
</SCRIPT> |
</HEAD> |
The script’s content is enclosed in an HTML comment by convention—it helps shield the client-parsed code from browsers that cannot interpret JavaScript, and though optional, should be used if you plan to re-use your code for other browsers.
Apple’s WebKit framework, and the Safari web browser based on it, both support the latest versions of JavaScript. Since the support is built into the framework, you can use all the features of JavaScript within anything that uses WebKit, including Safari, Dashboard, and any WebKit-based OS X application.
The Document Object Model (DOM) is a standardized software interface that allows code written in JavaScript and other languages to interact with the contents of an HTML document. The Document Object Model consists of a series of classes that represent HTML elements, events, and so on, each of which contains methods that operate on those elements or events.
With the Document Object Model, you can manipulate the contents of an HTML document in any number of ways, including adding, removing, and changing content, reading and altering the contents of a form, changing CSS styles (to hide or show content, for example), and so on.
By taking advantage of the Document Object Model, you can create much more dynamic websites that adapt as the user takes actions, such as showing certain form fields depending on selections in other fields, organizing your content based on what pages the viewer has recently visited, adding dynamic navigation features such as pull-down menus, and so on.
© 2004, 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-10-15)