JAVASCRIPT
What is JavaScript
HTML was designed to display document information on web pages. The resulting pages
were static by nature and boring thus there was a need for livelier pages to keep the interest of users. JavaScript was created
by Netscape for their Navigator 2.x (late 1995) to allow web pages to be more interactive. Although its name lures you into
believing that it is associated with Sun's Microsystems's Java, it has no semblance to Java. Sun and Netscape thought it would
help their marketing efforts to use similar names since Java was very popular at the time. The language was originally intended
for server side scripting but later versions has given developers capabilities for sophisticated server side scripting.
JavaScript is a simple, lightweight, interpreted, cross-platform programming language
with object oriented capabilities that can be written directly into HTML documents to facilitate a more interactive user interface
to a static HTML page. By inserting JavaScript programs into Web pages, authors can achieve simple multimedia effects like
image rollovers or add relatively sophisticated tools like calculators, mortgage calculators or forms handlers. It runs in
Web browsers like Netscape Communicator and Microsoft Internet Explorer and is in fact, the defacto scripting language for
HTML pages. The JavaScript language itself is standardized by Standard ECMA-262 ECMAScript: A general purpose, cross-platform
programming language.
The earliest version JavaScript was version 1.0. Subsequent versions were released
over the years and include 1.1, 1.2, 1.3 an the current version, 1.5. Version 1.4 was implemented only in Netscape server
products.
Core, Client Side and server side JavaScript
Core JavaScript contains a core set of objects, such as Array, Date, and Math, and
a core set of language elements such as operators, control structures, and statements. Core JavaScript is extended to include the client side and server side versions of the language by supplementing
it with additional objects.
Client-side JavaScript. Core JavaScript is extended by adding objects to control a browser and its Document Object
Model (DOM). For example, client-side extensions allow an application to place elements on an HTML form and respond to user
events such as mouse clicks, form input, and page navigation.
Server-side JavaScript. Core language is extended by adding the relevant objects for running JavaScript on a
server. For example, server-side extensions allow an application to communicate with a relational database, provide continuity
of information from one invocation to another of the application, or perform file manipulations on a server. Therefore, the server-side statements can connect to relational databases from
different vendors, share information across users of an application, access the file system on the server, or communicate
with other applications through LiveConnect and Java. HTML pages with server-side JavaScript can also include client-side
JavaScript.
Requirements
The JavaScript language is easy to learn and use. There are no requirements for any
expensive development tools to code applications. What is required is the knowledge of an easy to learn JavaScript language,
a free text editor like Notepad and a browser. Most operating systems come with a browser and JavaScript is the default
language used in HTML documents so there is no need for additional plug-ins. The following shows how to enable JavaScript
on Netscape Navigator and Internet Explorer.
Netscape:
Select the Edit tab.
Select Preferences.
In the left navigation box, select
the Advanced tab.
To enable, check the Enable JavaScript checkbox to the right.
Internet Explorer:
Select the View tab. (Internet Explorer 4.0)
Select the Tools tab (Internet
Explorer 5.0 and above)
Select Internet Options.
Select the Security tab.
Select Custom Level.
Select Settings.
Go to the Scripting Area.
To enable, check the Active Scripting checkbox.
JavaScript Features
- Facilitates a more interactive user interface
- Gives user more control over the browser
- Read and write client side with cookies
- Can be use to perform calculations as in currency converters,
mortgage calculators etc. on the client side
- Generating HTML pages on-the-fly without accessingthe Web
server
- Detects browser, OS, screen size, etc.
- Validating the user's input in forms
- Manage dates and time
Core JavaScript Basics
In HTML JavaScript is included in the <SCRIPT>&.</SCRIPT> tags or can
be saves as a separate file with the .js extension.
Statements
JavaScript use statements. Statements can be:
Basic as in x = y + z;
Compound as in if(y == 5) {z += 25}
A collection of statements
grouped together to be executed is referred to as a function.
Character Set
JavaScript uses the Unicode character set. This means that it uses two bytes to represent
every character in a string. JavaScript is case sensitive so programmers must be aware of any capitalization their code. By
default, all JavaScript commands are in lowercase. Alternatively, HTML is not case sensitive which could be a source of errors
for HTML pages. Hyphenated keywords from other languages that need to be addressed are written in C-style: border-right-color
as in CSS is written as borderRightColor in JavaScript.
White Spaces
JavaScript ignores white spaces and line breaks. Text strings across multiple lines
should be concatenated using the (+) operator.
Semicolon
All JavaScript statements ends with a semi-colon. If you have one statement per line,
it does not require a semicolon however, it is good practice to include one since it make sit easier for debugging and maintenance.
This would also eliminate the problems that may arise if statements run across multiple lines. A function that ends with a
curly bracket does not require a semicolon.
Comments
Comments im JavaScript work like C and C++
Use // for single line comments and /*&&.*/
for multiple line comments. JavaScript must deal with HTML comments also. While it recognizes the <!as the start of a comment
in HTML, it does not recognize the closing --> indicator thus must be commented as // &&&& -- >.
Literals
Numeric literals are written as is e.g. 125
String literals must be included in
balanced quotes e.g. 'JavaScript is it' or "queen". Double and single quotes are interchangeable but must be balanced. The
literal null does not require quotes.
Identifiers
Identifiers are names assigned to variables. They must begin with a letter, an underscore
or $. All characters should be valid ASCII characters. They cannot be the same as JavaScript reserved works.
Security Issues
Care was taken to build some security features into JavaScript. For example client-side
JavaScript cannot access network resources or write to filesystems. JavaScript Security Models are based upon Java (Powell
and Fritz Schneider). In theory, downloaded scripts are run by default in a restricted 'sandbox' environment that isolates
them from the rest of the operating system. Scripts are permitted access only to data in the current document or closely related
documents (generally those from the same site as the current document). No access is granted to the local file system, the
memory space of other running programs, or the operating system’s networking layer. Containment of this kind is designed
to prevent malfunctioning or malicious scripts from wreaking havoc in the user’s environment. Nonetheless, some malicious
scripts escape the sandbox and proceed to install spyware and trojans. The safest way is to disable scripting but then you
would miss out on the dynamic features scripting offers. Similar issues exist for VBScript
VB-SCRIPT
What is VBScript
Because of the static nature of HTML only documents, scripting languages were designed
to introduce some level of interactivity in these web pages. VBScript is Microsoft's solution to the problem.
VBScript, Microsoft's Visual Basic Scripting Edition, is a scaled down version of Visual Basic for use with web pages and
other applications that uses Microsoft Active X controls. VBScript is very similar in use and syntax to JavaScript except
it is based on a subset of Microsoft's Visual Basic language While it doesn't offer the functionality of Visual Basic, it
does provide a powerful, easy to learn tool that can be used to add interaction to your web pages. Users who already have
experience with either versions of Visual Basic should find VBScript easy to work with.
Requirements
As long as scripting is turned on in the browser, VS scripts can be added to HTML documents
by indicating to the browser that VB scripts are included. Use the type attribute of the <script> tag to define the
scripting language.
<script type="text/vbscript">
< Model Object Document the of support needs JavaScript Like requirements. per as
are requirements other>
Comparison of JavaScript and VBScript
Similarities
- Both languages are easy to learn and do not require any expensive
development tools
- Both can be used to enhance web pages
- They run on client machines and can substitute CGI programs
to reduce server loads
- Both can abuse and run malicious scripts on clients' machines
Differences
- JavaScript is the default scripting language for browsers
but VBScript must be specified as the scripting language.
- JavaScript has cross-platform support from all popular browsers
while VBScript is supported MS IE only. VBScripters would thus loose a sizable audience.
- One of the most significant issues with JavaScript is that
there were different releases of the language since its inception (version 1.0). Similarly, different versions of browsers
exist on users machines. Therefore, code written for one version may not necessarily work on another. More testing would be
necessary thus, development time increases.
- JavaScript is case sensitive but VBScript is not this would
not be prone to as many syntax errors.<
- JavaScript uses the same character for concatenation as it
does for addition (the + character) while the '&' concatenating character is used in VBScript. This is another source
of errors in JavaScript.