User:Jesdisciple/JavaScript/Introduction

From Wikibooks, open books for an open world
Jump to: navigation, search
Previous: JavaScript Index Next: Placing the Code

JavaScript is an interpreted programming language that is mostly used to turn static web pages into dynamic and interactive pages after a web browser has finished downloading them. For example, a clock displayed on a page that updates itself to show the current time on the user's computer. JavaScript's design was influenced by many programming languages, including C, but is meant to be more usable by non-programmers. JavaScript is not based on or related to Java; this is a common misunderstanding which you can read more about here.

Before we start[edit]

To write JavaScript, you will need to choose a plain text editor (Notepad, Notepad++, PSPad...) and a web browser . It is a good idea to have a wide array of browsers available for testing, as they often behave in incompatible ways. You should also be fluent in HTML or XHTML, because manipulating them is the only useful thing JavaScript does in a web page.

To test your programs, you will need an up-to-date web browser. To make them work for the greatest number of users, test them in several browsers. Each browser has slightly different rendering, and most have their quirks which cause technically correct JavaScript to break. Popular browsers include Firefox, Opera, Chrome, and Safari. Internet Explorer, although the pet peeve of many web developers, is the most popular among users and therefore essential to your testing kit. A text-only browser such as Lynx would also serve you well, as it presents the same information in the same order as a seeing- or hearing-impaired user's browser does.

Be aware that JavaScript is unsuitable for essential logic, i.e. anything that must run or that contains secret information. Not all browsers have a JavaScript interpreter (such as text-only browsers), or run its latest version. Some users turn off JavaScript capabilities by choice, and as you will soon learn JavaScript source may be viewed and altered by anyone just like HTML. Generally, web pages should only use JavaScript to enhance the user's experience, rather than depend on it. This is often referred to as graceful degradation (i.e if the user has turned off JavaScript, the page should always still load, presenting the same information but without the extra functionality provided by JavaScript.) A server-side language is a much better tool for more critical purposes.

Your first program[edit]

Let's start with a simple program (modified from the HTML tutorial. Write this code in your editor, or copy-and-paste it, and save it as "index.html". The file must be saved with the exact extension, and will not render or run correctly otherwise.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <title> My First Program </title>
        <script type="text/javascript">
            alert('Hello, world! You just lost The Game.');
        </script>
    </head>
    <body>
        <p><a href="http://en.wikipedia.org/wiki/The_Game_%28mind_game%29">The Game</a> is a mind game
        where the objective is to avoid thinking about The Game itself. Thinking about The Game
        constitutes a loss, which, according to the rules of The Game, must be announced each time it
        occurs. It is impossible to win The Game; players can only attempt to avoid losing for as long
        as possible. The Game has been described alternately as pointless and infuriating, or as a
        challenging game that is fun to play. As of 2010, The Game is played by millions worldwide.</p>
    </body>
</html>

Now open the document in your browser and see the result. Notice that the source code is normal HTML, except for the script tag which contains the JavaScript. We'll explore its contents in the coming chapters.

Previous: JavaScript Index Next: Placing the Code