What is Javascript?

The language was invented by Brendan Eich at Netscape (with Navigator 2.0), and has appeared in all Netscape and Microsoft browsers since the development of ECMA-262 started in 1996, and the first edition of was adopted by the ECMA General Assembly in June 1997. JavaScript’s official name is ECMAScript and is developed and maintained by the ECMA organization.

The development of the standard is still in progress.

  • JavaScript was designed to add interactivity to HTML pages
  • JavaScript is a scripting language
  • A scripting language is a lightweight programming language
  • JavaScript is usually embedded directly into HTML pages
  • JavaScript is an interpreted language (means that scripts execute without preliminary compilation)
  • Everyone can use JavaScript without purchasing a license

What can a JavaScript do?

  • JavaScript can put dynamic text into an HTML page
  • A JavaScript statement like this: document.write("

    ” + name + “

    “) can write a variable text into an HTML page

  • A JavaScript can be set to execute when something happens, like when a page has finished loading or when a user clicks on an HTML element
  • A JavaScript can read and change the content of an HTML element
  • A JavaScript can be used to validate form data before it is submitted to a server
  • A JavaScript can be used to detect the visitor’s browser, and load another page specifically designed for that browser
  • A JavaScript can be used to store and retrieve information on the visitor’s computer

To insert a JavaScript into an HTML page, we use the tags, the browser will recognize it as a JavaScript command and execute the code line. In this case the browser will write Hello World! to the page:

So, the tells where the JavaScript starts and ends:

<script type="text/javascript">
document.write("Hello World!");
</script>

In your browser:

Adding script to the <head>

Scripts to be executed when they are called, or when an event is triggered, are placed in functions. Put your functions in the head section, this way they are all in one place, and they do not interfere with page content.

<head>
<script type="text/javascript">
function message()
{
alert("This alert box was called with the onload event");
}
</script>
</head>

Scripts in the <head>and<body>

You can place an unlimited number of scripts in your document, so you can have scripts in both the body and the head section.

<head>
<script type="text/javascript">
function message()
{
alert("This alert box was called with the onload event");
}
</script>
</head>

<body onload="message()">
<script type="text/javascript">
document.write("This message is written by JavaScript");
</script>
</body>

JavaScript Blocks

JavaScript statements can be grouped together in blocks. Blocks start with a left curly bracket {, and ends with a right curly bracket }. The purpose of a block is to make the sequence of statements execute together.

<script type="text/javascript">
// Write a heading
document.write("<h1>This is a heading</h1>");
// Write two paragraphs:
document.write("<p>This is a paragraph.</p>");
document.write("<p>This is another paragraph.</p>");
</script>

For more on this topic, read:

Beginning HTML, XHTML, CSS, and Javascript

Author: Jon Dockett
Chapter: 11 Learning Javascript
pages: 482-488

Print this entry