Introduction to Java Script
(Page 1 of 4 )
Many times when you are building a web page you might need more than just PHP and HTML. Java Script can fill that gap. In this tutorial, John Johnston will get you started with Java Script.
By : John Johnston
Java Script, not the same thing as Java which is an entirely different language and used for very different reasons, is a web based scripting (programming ) language developed by Netscape and is used to add interactivity to web pages. While it can be simple to use, like all programming languages, JavaScript follows strict syntax rules that you cannot break. If you don’t follow the syntax, it will not work.
How Does Java Script Work?Java Script works by telling your browser that (1) it is a language on your browser, and (2) what to do with the scripts that you write. This is of course, over simplified, but we are not developers of languages, nor senior programmers, so we aren’t going to concern ourselves with how this all works. What we will consider is how to make it work and which browsers will and won’t use Java Script.
Does Java Script Work on all Browsers?Unfortunately, no. While most versions of Netscape and Internet Explorer will work ( from version 3 on for both ), some browsers simply don’t understand Java Script and as such, we are required to hide these scripts from those browsers so that people don’t hunt us down and complain. To make things even more complicated, not all browsers handle JavaScript in the same manner. While the end result is the same, how the browser does it may be entirely different, even in the same version of a browser! Scared? Don’t be. Java Script can be the simpelist and easiest way to handle all your web based programming needs.
Now some of you might ask about those browsers that don’t handle JavaScript. In essence, these browsers would just spit out all the scripting you have done as text in your HTML page. This would confuse everyone. Luckily, HTML has a built in way to comment, or hide, these things. In this way, we can hide our Script from browsers that cannot read it, and still let the browsers that can use it! How? Take a look below.
<html> <head> <title> Title of your page </title> <script language="JavaScript"> <!-- hide this stuff from other browsers YOUR SCRIPT HERE // end the hiding comment --> </script> </head> |
You should notice several things right off the bat. First, we have to tell our browser that we are using Java Script. ( <script language="JavaScript">). While Java Script is the default scripting language for browsers, this might not always be the case, and it is a good idea to keep it in there. The next thing that you should notice is that we have hidden our script from the browsers that can’t handle it with ( <!-- something //-->). The third thing that you should notice is that this script is placed entirely within the head of the HTML document. Most of your scripting will happen here. While you can, and will, use JavaScript in the body of your document, all of the really important programming will occur in the head. Finally, you should notice that just like basic HTML, you must open and close your script tags.
A Note on ProgrammingSome of the most important things about programming are not necessarily its function, but rather it’s format. Just like writing a research paper, the greatest paper in the world will do you no good if other people can’t read and understand it. Programming is no different. Form and style do count. The single easiest way to make sure that people understand what you are doing and why is to comment out areas of your script. This acts as a simple way to note things in your script so that it is easy to understand for those who want to steal your scripts…or for scripts that you want to steal yourself! To comment out a section just follow the format below.
/* this is a huge block of text that I've commented out */ |
Anything placed between the /* blah blah blah */ will be ignored by both HTML and JavaScript. With that let’s start some scripting!
Next: Variables >>
More Miscellaneous Articles
More By Codewalkers