This section will provide you with the basics of what JavaScript is, and why you should use it.
Objectives
• To examine the Evolution of JavaScript and describe some of its key features
• To explain JavaScript fundamentals
• To understand difference between Interpreted programs versus Compiled programs
• To explain why to use JavaScript
HTML, DHTML & JavaScript
History of JavaScript
Web pages made using only HTML are somewhat static with no interactivity and negligible user involvement. Interactive pages can be described as those that understand and process user behaviour (actions) like mouse movements, mouse button clicks, HTML button clicks etc.
HTML tags are just instructions on document layout and structure; the display of the document in the window is dependent on the browser. Interactive pages cannot be built with only HTML, we need a programming language. Further, since the response time to user actions should be quick, the programming language has to be embedded in the browser itself. (Programs residing on servers can not bring about interactivity due to the time taken for sending the user input and receiving the response.) Keeping these things in mind, the people at Netscape came out with a client-side (read browser) language that was integrated with version 2 of Netscape Navigator. They called this language JavaScript. (Actually, JavaScript was initially christened LiveScript. Seeing the popularity gained by Sun Systems' Java, the bigwigs at Netscape changed its name... a clever marketing policy!) Microsoft's implementation of JavaScript is called JScript and was first introduced with version 3 of
Internet Explorer.
JavaScript is thus, a client-side scripting language. There is also a server-side version that is used in Active Server Pages, a technology promoted by Microsoft. This tutorial describes only client-side JavaScript.
JavaScript enables browsers to take decisions and process information. This is the key to interactivity.
JavaScript versus JAVA
JAVA is a strongly typed, compiled programming language developed by Sun Microsystems. JavaScript, developed originally by Netscape, is a lightweight, interpreted programming language initially called LiveScript. The two languages are not related in any way. All programming languages share a certain amount of similarity.
Interpreted programs versus Compiled programs
Before we start discussing the differences between interpreted and compiled we have to define the term source code or as it is more commonly referred to, the code. The code is the plain text commands that the program is written in. All programming languages start out as source code; it is then either interpreted or compiled. The code that you will create in this course can be considered source code.
Interpreted programming languages tend to be simpler to program but slower to execute in general.
Each time a program is run it has to be interpreted (interrogated) line by line, based on the flow of execution (you will see later branches and loops affect the flow of execution).
Compiled programming languages have a more complex syntax, and require more strict programming practices. With a compiled programming language you first write the source code, and then you feed it to a compiler (a special computer program) which produces an executable binary program. On the Windows platforms the output of the compiler usually ends in the ".exe" file extension. The program that comes out of the compilation process tends to be platform (operating system) specific. The key benefit for the programmer is that no other programmer can look at the source code once it is compiled. The other key factor is that the language used to write the source code becomes irrelevant once it has been compiled.
JAVA is a compiled language that is platform independent, whereas JavaScript is an interpreted language. The browser provides the platform independence for JAVA through its JAVA Virtual Machine, and the interpreter for JavaScript. As a result, the browser you are writing your scripts for is important.
Why to Learn JavaScript
HTML, DHTML & JavaScript
What you can use JavaScript for
Here are a few things you can do with JavaScript:
• Validate forms at the client-side saving both the precious server resources and time.
• Create mouseover effects, change background colour of a document with a click of a button... interactivity!
• Randomly display content without the involvement of server programs.
• Move HTML elements around pages.
• Change page contents dynamically.
• Load content in new browser windows and frames.
• Make online games.
Nature of JavaScript
JavaScript is based on Object Oriented Programming concept. Its syntax is quite similar to C, C++ and Java. As it is based on Object Oriented Programming concepts, it is called as “Object Based”.
However, it is much easier to learn and implement.
Other Client-side languages like JavaScript
JScript: Invented by Microsoft, this language is similar to JavaScript. Supported by almost all the browsers.
VBScript: Implemented only under Internet Explorer, this client-side language is similar to BASIC. It is commonly used in ASP technology.
PerlScript: Similar to the Perl Language. Not well supported by all browsers.
HTML, DHTML & JavaScript
Review Questions
1. State True or False: JavaScript is an interpreted language.
2. JAVA is a ______________ programming language, and is platform ____________.
3. State True or False: JAVA and JavaScript were created by the same company.
4. Microsoft Internet Explorer supports the following scripting languages.
a. JavaScript b. JAVA c. BASIC d. VBScript e. C++
f. Perl
5. State True or False: JavaScript is supported by a large number of browsers.
HTML, DHTML & JavaScript
Summary
JavaScript is not JAVA. JavaScript is Interpreted, and JAVA is Compiled programming language. Probably we would use JavaScript for various activities on webpage. JavaScript Language is not so powerful like any other programming language.
HTML, DHTML & JavaScript