IWATS - JavaScript

Introduction

Welcome!

Welcome to the IWATS JavaScript course! Here you will learn the basics of JavaScript. If you already know how to use HTML to create Web pages, than you have already worked with one type of computer language. JavaScript is the easiest of these languages to spice up a webpage and add interactivity with the user. This interactivity enables the Web page creator to dynamically control the web page elements. You can use JavaScript to change Web page elements and interact with the the user. Just a few examples of what JavaScript is capable of are; validating a form, making calculations, displaying messages to the user, mouseovers, detecting the users browser and altering the page to fit their specific needs or even as simple as displaying the date at the top of a webpage.

History of JavaScript

Developed by Netscape Communications Corporation, better known for their development of the Netscape Web browser which bares their name, JavaScript was the first Web scripting language to come into use on the Web and remains the most popular. Originally called LiveScript, JavaScript was introduced in 1995 within Netscape Navigator 2.0.  The name was then changed to what it is known as today, JavaScript, to help market the language because of its relationship with another popular computer language Java.  Note that Java and JavaScript, while containing similar syntax, are not the same thing.

Advantages and Disadvantages

As with anything in this world, JavaScript contains both advantages and disadvantages. Understanding these will help you make a decision about using JavaScript for a potential project. Here's the advantages and disadvantages of using JavaScript in your code:

Advantages:
  • Supported by both Netscape and Internet Explorer.
  • The most popular client-based Web scripting language.
  • Can run in a browser without any additional tools.
  • Doesn't require any server-side programs.
Disadvantages:
  • Code can be seen by anyone viewing the source of your page.
  • Database interaction isn't done with ease, if done at all.

Just to emphasize, JavaScript is a client-side language. This is very different from PHP and ASP, which require a parser on the server-side. JavaScript only requires a browser that knows what to do with its functions, and almost every major browser does. Now let's move on in our intrepid journey through the basics of JavaScript.

Where does JavaScript go?

You can use JavaScript almost anywhere within your document, and every document consists of a header inside the <head> tag and a body which is inside the <body> tag.  Within this document, if you wanted to add content to the head you would have to put it inside this <head> tag, same with the body.  Just like every document, if you want to add JavaScript to your webpage, you will use the <script> tag.  Whenever you use the <script> tag the browser knows that you want to start treating the text inside the tag as script.  After you finished the script, you will use a closing tag, </script>.  Within the script tag itself you specify the language and type of script you are using, as well as version if you wish, by modifying the language and type parameters on the tag. An example which would include the date which your page was last modified is shown below in Figure 1.0.

Figure 1.0
<script language="JavaScript" type="text/javascript">
document.write(document.lastModified);
</script>

If you want to use JavaScript in your document you almost always have to place this code inside the <script> tag. The exception to this rule would be with event handlers, which we will discuss later on in this test.  There are three places where the script tag can be used within an html document and only three, these are.

  1. Inside the <body> tags of an html document to be read and displayed by the browser when the webpage loads. This is like in Figure 1.0 above, where placing that script within the body of a page would display when the page was last modified to the user. 
     
  2. The script tag can also be used in the header of a file, within the <head> tags.  Scripts that are placed within the head of a document cannot be displayed by the browser, however functions and variables declared there can be referred to by scripts elsewhere inside the body tags.  We will learn more about these later on in this test.
     
  3. Script can also be used in an entirely separate file, and then linked to from within the html document. Since JavaScript uses the .js extention, these files can contain code in a file separate from the webpage but linked to.  This is a great advantage if you are going to use the same code over many webpages. Creating the code once and using it many times. Refer to Figure 1.1 for an example of linking to an external JavaScript file in the header of a webpage. 
Figure 1.1
<script language="JavaScript" type="text/javascript" src="filename.js">
</script>

JavaScript has many different very powerful uses within an html document, however it is not the only Web scripting language in use.  Determining if it is the right tool for the job requires understanding what other tools are available.  A couple of these tools include Java, ActiveX, Visual Basic, CGI and there are more.  What these other languages are capable of will not be discussed in this test, but just know they are out there and are capable of some things JavaScript isn't.