It is a language, a scripting language. For those of you who are new in the material scripting language means that JavaScript is written and run in the form of a script (which is actually just simple text). Just to mention scripting languages like JavaScript are VBScript, Perl, PHP, etc. There are other languages like Java, C/C++, Pascal which use programs called compilers to compile (translate the code on the language of the computer) their programs into executable files and the output form of the programs is not script.
So JavaScript was developed by Netscape and although many people think so it has nothing to do with the popular language Java (except for the name) which is actually developed by Sun Microsystems. JavaScript is built as a part of your browser (Microsoft first built it into its Internet Explorer 3.0 and Netscape did it in its Netscape Navigator 2.0) and web developers embed it in their pages to create interactivity and impression of dynamics.
Adding a JavaScript to Web page?
It's absolutely easy to create a JavaScript and run it. All you need is a text editor. You just write the code in it. Then you create a Web page and embed the code directly in it. The JavaScript will run only if you enclose the code with <script> in the beginning and </script> at the end. So a simple script will look like this:
<html>
<body>
Your HTML page somewhere here.
<script language=JavaScript>
alert('Hello! I wrote my first JavaScript')
</script>
Your HTML page again.
</body>
</html>
If you want to see what will be the output just copy it in your favourite text editor and save the page as hello.html or whatever you want (the file must just finish on ".html", ".htm", ".shtml",etc.). Another way to add a JavaScipt in a Web page is to write it in a separate file. In this case you should save the JavaScript in a file with .js extension and put the following code in the page:
<script language=JavaScript src=hello.js>
</script>
If you wander what language=JavaScript represent, all it does is telling the browser what type of script to expect. This could be omitted in Internet Explorer or Netscape Navigator because their default languages are JavaScript and they will run the script even without this.
You could also put the language= attribute with "JavaScript 1.1" or "JavaScript 1.2" if your script is written for a particular JavaScript version and you want to prevent old browsers from crashing because they do not support this JavaScript version. For example if you have written a script using JavaScript 1.2 (By the way the version of JavaScript at the time I am writing this tutorial is 1.5) which is built in browsers of version 4.0 or higher and you don't want Internet Explorer 3.0 to crash in any way you just put <script language="JavaScript 1.2"> the IE 3 will ignore this script because it has not support for JavaScript 1.2. If you wander what version of JavaScript support your browser here is a brief list of versions:
- JavaScript 1.0 - NS 2.0 and IE 3.0
- JavaScript 1.1 - NS 3.0 and IE 3.0
- JavaScript 1.2 - NS 4.0 and IE 4.0
- JavaScript 1.3 - NS 4.6 and IE 5.0
- JavaScript 1.5 - NS 6.0 Gecko
If you are not sure for what browser to create the JavaScript another way to prevent errors on your script on older browsers is to enclose it with the common HTML comments. After the <script> tag you have to put <!-- and at the end of the script just before the closing </script> you need to put //-->. So the script above with all these "prevent-error" things will look like this:
<script language="JavaScript 1.2">
<!--
alert('Hello! I wrote my first JavaScript')
//-->
</script>
So these are the basics of JavaScript and I will start will the structure of the JavaScript language but before I continue I will just mention that JavaScipt is a case sensitive language which means that for example alert() and ALERT() are different things (as a matter of fact the second one will return error "Object Expected" unless in the script there is a function called ALERT but for that later).
Continued...