Javascript Introduction:
What is Javascript?
JavaScript is an object-oriented scripting
language developed by Netscape that allows you to create web pages. Javascript is a client-side scripting language. This means the user's browser will be running the script. This is opposite to client-side is server-side, which occurs in a language like PHP. These PHP scripts are run by the web hosting server. JavaScript
is not Java.
JavaScript
and Java are similar in some ways but fundamentally different in others. Java
is a compiled programming language, similar to languages like C, C++, or Pascal.
JavaScript is a
scripting language, meant for small programs used only from within web browsers.
A JavaScript script can't run alone, without the browser, the way a Java program
does. JScript is Microsoft's version of JavaScript.
Top
Embedding JavaScript into a HTML-page JavaScript
code is embedded directly into a HTML-page using <script> and
</script> tags. It is possible to embed many scripts into
a single document , using multiple SCRIPT tags.
A
typical format for a javascript embedded into a HTML file would look like :
<script
="JavaScript">
<!--
Javascript statements
.
.
//-->
</script>
JavaScript is
case sensitive:
Unlike HTML, Javascript is case-sensitive.
Top
Specifying the JavaScript Version
As mentioned above,
there are several versions of JavaScript supported by certain browsers and browser
versions. You can specify the javascript version using
the LANGUAGE attribute.
Eg:
<script language ="JavaScript1.1">
<!--
Javascript statements
//-->
</script>
Statements
within a <SCRIPT> tag are ignored if the browser does not support the javascript
version specified in the LANGUAGE attribute. In other words, you can specify that
a section of code only be executed by browsers which support a particular
version of JavaScript.
Eg:
1)Navigator 2.0 executes code within the <SCRIPT LANGUAGE="JavaScript">
tag; it ignores
code within the <SCRIPT LANGUAGE="JavaScript1.1">
and <SCRIPT
LANGUAGE="JavaScript1.2"> tags.
2) Navigator 3.0 executes code within the <SCRIPT
LANGUAGE="JavaScript"> and <SCRIPT
LANGUAGE="JavaScript1.1"> tags; it ignores code within
the <SCRIPT
LANGUAGE="JavaScript1.2"> tag.
3) Navigator 4.0 executes code within the <SCRIPT
LANGUAGE="JavaScript">, <SCRIPT
LANGUAGE="JavaScript1.1">, and <SCRIPT LANGUAGE="JavaScript1.2">
Top
Specifying a file for Javascript Code You
can include an external file containing JavaScript code into a HTML file. SRC,
another attribute of the SCRIPT tag is used for this purpose. This
feature is helpful when you want to hide your javascript code from others (HTML
Source code of all web pages can be seen from the borwser. Using this feature
you can hide your javascript code from others)
For
example: You can have all your javascript statements in a file called 'myjavascript.js'
and can include into your HTML file as shown below:
<HEAD>
<TITLE>My
Javascript Page</TITLE>
<SCRIPT SRC="myjavascript.js">
</SCRIPT>
</HEAD>
<BODY>
HTML Code
.
.
.
<BODY>
The
external file is simply a text file containing only JavaScript statements and
function definitions and filename ends with the extension ".js". The
SRC attribute can specify any URL, relative or absolute. All JavaScript statements
within a <SCRIPT> tag with a SRC attribute are ignored (unless there was an
error in file inclusion).
Top
Hiding Scripts within Comment Tags Only
Netscape Navigator versions 2.0 and later recognize JavaScript. If a browser does
not recognize javascript code, the whole code in displayed when you see the HTML
document. To ensure that your JavaScript code is not displayed by old browsers,
place Javascript inside comment fields. The markup to begin a comment field
is <!-- while you close a comment field using //-->.
Eg:
<SCRIPT>
<!--
Hide script contents from old browsers that does not javascript.
Include
JavaScript statements...
// Finish hiding here. -->
</SCRIPT>
Top
About Focus on JavaScript
Focus on JavaScript
All My Scripts
22 Nov 2008 at 3:42am
How many JavaScripts can one person write?
I have now added so many scripts to this site that even I have started to have trouble keeping track of all of what...
Alternative Ways to Define Functions
21 Nov 2008 at 9:22am
JavaScript gives you three different ways to define a function.
There is more than one way to define a function in JavaScript - in fact there are three. Each works slightly...
Accessibility
20 Nov 2008 at 3:41am
Coding your JavaScript the right way can enhance your visitor's experience of your site.
Not all of the people visiting your site will have JavaScript enabled. If you code your web...
Timed Events
19 Nov 2008 at 2:21am
You delay processing in JavaScript by triggering it after a specified time.
Not all of the events your page handles need to be triggered by your visitors actions. You can set...
Getting Rid of Noscript
18 Nov 2008 at 1:28am
Some people think using noscript makes a page more accessible, the reverse is true.
Anonymous responses that I received regarding prior articles on getting rid of the noscript tag demonstrate that...
The Screen
17 Nov 2008 at 2:23am
A lot of people seem concerned about screen resolution when it comes to planning their page layout.
The browser viewport is where your web page displays and that is not the...
ASCII and Unicode
16 Nov 2008 at 10:56am
JavaScript Regular Expressions can test any character at all.
The twelfth tutorial on Regular Expressions shows you how to include any character at all in your JavaScript Regular Expression by specifying...
Calendar
15 Nov 2008 at 10:08am
Old JavaScripts never die, they just get rewritten to work better
I originally wrote my calendar script back in 2000 and have revisited it several times since then to rework the...
Time of Day
14 Nov 2008 at 8:43am
JavaScript can change the appearance of a web page based on the time of day at your visitor's location.
People are more likely to return to a web page if it...
Day of Week Image
13 Nov 2008 at 8:51am
JavaScript can do different things for different days of the week
If someone who visits your site today returns tomorrow will your site look different? It will if you use this...