Getting Started with JavaScript

Introduction

To get started programming with JavaScript, you first have to write few scripts. Now there are a few ways you can do this with HTML and Javascript.

As you might guess, the examples on this site will use the last method. Its just a lot simpler show and provide the source for the examples if everything is in one file.

A Base JavaScript Example

Examples for this site use the basic structure shown below. Explanations of the code follow the example.

jsbase.html (Sample) (Source)
   1:<html>
   2:  <head>
   3:    <title>JavaScript Base File</title>
   4:    <script type="text/javascript">
   5:      function main(){
   6:        /* Define and print array normal */
   7:        var myStr = "<p>Hello World!</p>";
   8:        var div1 = document.getElementById("div1");
   9:        div1.innerHTML = myStr;
  10:
  11:      }
  12:    </script>
  13:  </head>
  14:<body onLoad="main()">
  15:  <h2>JavaScript Base File</h2>
  16:  <h4>Example Output</h4>
  17:  <div id="div1">
  18:  </div>
  19:</body>
  20:</html>

Here are some of the key points.

If you click on the link and look at the source file. You will see that the hello message is inserted into the HTML document. With this basic setup, you can explore many of the features of JavaScript in a browser.