Some Background on JavaScript

 

Introduction.  There are a large variety of uses for JavaScript which was created by Netscape in order to add interactivity to web sites.  It first came out with Netscape 2.0.  The syllabus contains a fairly long list of topics we will cover in this course.  It has been developed to be processed by the browser on the client's computer in typical client-web server interaction.  JavaScript code is downloaded along with the HTML and images when a request is made to the web server.   It isn't enacted until the web browser executes it.

JavaScript can be placed in either the <body> or <head> of your HTML code, though it is generally better to place it in the head to make it easier to find.  You need to delimit any code with a <script> </script> tag pair.  Everything with any such pair of tags is interpreted by the browser as JavaScript.  Even if it encounters the simplest HTML within these delimiters it causes execution errors.

The basic structure of a web page including JavaScript is contained in the following table.

 

<html>
<head>

<title>JavaScript Skeleton</title>

<script language="JavaScript">

//  Now you can place your JavaScript here
//  But no HTML!!!!

</script>

</head>

<body>

<script language="JavaScript">

//  You can place your JavaScript here also
//  But no HTML!!!!

</script>

</body>
</html>

 

If the browser that is trying to implement your JavaScript is older than Netscape 2.0 or Internet Explorer 3.0, you may want to do some additional coding.  In older browsers, the JavaScript is passed to the screen as if it was plain text.

In order to make sure your JavaScript isn't written to the browser screen due to the client having an older browser you need to enclose the JavaScript in HTML comment delimiters within the <script> tag pair.  Notice the following.

 

<script language="JavaScript">

<!-- hides the code from older browsers

//  Now you can place your JavaScript here

end hiding the JavaScript -->

</script>

 

We are not going to do this hiding in this course.  This is just to demonstrate what you should do if you want to make your code less obtrusive.

Your First JavaScript.  Hello World programs have become a standard for first learning computer languages.  I first saw it years ago when learning C.

Copy the following code into a file called MyFirstJavaScript.html and upload it to your web space and view it.

 

<html>
<head>

<title>JavaScript Skeleton</title>

</head>

<body>

<script language="JavaScript">

//  say Hello World!
alert("Hello World");

</script>

</body>
</html>

 

When you upload and execute this you should get a message box or alert like the following.