This page requires Javascript support!

Javascript



Hello everyone! Below you will learn how to program in Javascript. I will go through and explain what the code is used for, show you the code, and afterwards give you a working example. This tutorial is for those who already know the basic principles behind Javascript programming. The following pages are going to be free of any needless graphics in order to optimize the speed in which they load. Have fun!!

This first example will show you the location of where to put code in your HTML document:

<HTML>
<HEAD>
<TITLE>My first script</TITLE>
</HEAD>
<BODY BGCOLOR=WHITE>
<H1>
<SCRIPT LANGUAGE=JAVASCRIPT
TYPE="TEXT/JAVASCRIPT">

document.write("Hello, world!")

</SCRIPT>
</H1>
</BODY>
</HTML>

Script can be either placed between the <HEAD> and </HEAD> tags or between the <BODY> and </BODY> tags.



This next section will teach you how to hide scripts from older browsers, which include Netscape 1.x and Microsoft Internet Explorer versions 3 and under. This following examples are going to be written without the preliminary HTML tags:


<SCRIPT LANGUAGE=JAVASCRIPT
TYPE="TEXT/JAVASCRIPT">

<!-- Hide script from old browsers

document.write("Hello, world!")

// End hiding script from old browsers -->
</SCRIPT>

Click HERE for a working example.

Comments can also be added within the body of the script by placing the words between a /* and a */. Here is an example:


<SCRIPT LANGUAGE=JAVASCRIPT
TYPE="TEXT/JAVASCRIPT">

<!-- Hide script from old browsers

/*this is a comment that the user will not see unless they are looking directly at the code*/

document.write("Hello, world!")

// End hiding script from old browsers -->
</SCRIPT>


You can provide feedback to people that are using your site by alerting them using Javascript. Here is an example:


<SCRIPT LANGUAGE=JAVASCRIPT
TYPE="TEXT/JAVASCRIPT">

<!-- Hide script from old browsers

alert("You will like this page!")

// End hiding script from old browsers -->
</SCRIPT>

Just put the text that you want to alert the user with in parenthesis using quotes. Click HERE for a working example.



The next thing I'll cover is using Javascript to detect a browser and perform conditionals which performs different actions depending on the test. Here is an example:

<SCRIPT LANGUAGE=JAVASCRIPT
TYPE="TEXT/JAVASCRIPT">

<!-- Hide script from old browsers

if (navigator.appName == "Netscape") {
document.write("You apparently have Netscape!")
}
else {
document.write("You don't have Netscape?")
}

// End hiding script from old browsers -->
</SCRIPT>

This code checks if the user has Netscape or not by using an if-else statement and redirects them to the appropriate page. This example checks the apllication name. If the Javascript checks the browser name and it is Netscape then it goes to the document.write function and prints the text on the screen. If it isn't Netscape the writes the document.write in the else statement. Click HERE for a working example.


MORE