» » » » » Computer programming in two minutes – JavaScript

Computer programming in two minutes – JavaScript

JavaScript

JavaScript is a language executed by the web browser to display a web page (it is mostly used for that).

Create the HTML document (the file name usually ends with .html) and open it with a browser.

<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”>
<html>
<head>
<script type=”text/javascript”>

alert(prompt(“bonjour le monde !”));

</script>
</head>

<body>
Page content.
</body>
</html>

The browser prompts and alerts then displays the document body. Congratulations, you’ve written your first JavaScript statements in an HTML document!

Note: Most browsers run without problem this simplified code:

<body>
Page content.
<script type=”text/javascript”>
alert(prompt(“Hello world!”));
</script>
</body>

One minute

A web page is a document consisting of a head and a body (<html> <head> </ head> <body> </ body> </ html>).

The body of the document is displayed on the screen while the head contains the title of the page, its favicon, its keywords, etc. (for example, view the source code of this page (you can often use the keyboard shortcut Ctrl + U))

The head and body of a html document can contain JavaScript statements:

<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”>
<html>
<head>
</head>

<body>
Content.

<script type=”text/javascript”>
alert(prompt(“Hello World!”));
</script>
</body>
</html>

At the opening of this document, the browser displays the body then prompts and alerts.

One minute and a half: a JavaScript function

<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”>
<html>
<head>
<script type=”text/javascript”>
function Prompt_and_alert() {
alert(prompt(“Hello World!”));
}
</script>
</head>

<body>
<a onmouseout=”Prompt_and_alert();”> <!– an anchor tag a hyperlink –>
Content.
</a>

</body>
</html>

The function is defined in the document header and is executed when the mouse leaves the anchor (onmouseout).

Two minutes

To go further, grab your browser with a tool such as Firebug.

To learn how to handle this tool, enter an error in your document, such as forgetting a quotation marks , and check for the error in the JavaScript console.

Congratulations, now you are ready to write further instructions!

Leave a Reply

Your email address will not be published. Required fields are marked *