Computer & Internet Tricks For You

 

What Is JavaScript 


JavaScript is an easy-to-use object-oriented scripting language designed for creating dynamic applications that link resources on both clients and servers.JavaScript called as the glue that holds Web pages together. While Java is used by programmers to create new objects and applets,

JavaScript is designed for enterprise application developers to dynamically script the behavior of objects running on either the client or the server. Every commercial Web page, or almost any Web page, that contain some JavaScript code. JavaScript is:

* Designed for creating network-centric applications

* Complementary to and integrated with Java

* Complementary to and integrated with HTML

* Open and cross-platform

This means that JavaScript will work on any platform (Windows, Linux, Mac, etc.). You don’t need any special tools to use JavaScript. You can write scripts in any text editor.

JavaScript, previously called LiveScript, it was developed by Brendan Eich at Netscape in 1995 and was shipped with Netscape Navigator 2 beta releases. LiveScript is hardware independent. JavaScript is built directly into the browser. In syntax, JavaScript is similar to C programming, Perl, and Java; e.g., if statements and for loops and while, are almost identical in java script. Like Perl, it is an object-oriented, interpreted language, not a compiled language.

Because JavaScript is completely associated with in a browser, it is integrated with HTML. JavaScript programs are executed by a JavaScript interpreter normally built right into the browser. When ever the Web browser requests a page, the server sends the full document, including HTML and JavaScript statements, over the internet to the client. When the page loads on the client side, HTML content is read line by line until a JavaScript opening tag is read, at which time the JavaScript interpreter takes over. When the closing JavaScript tag is reached, the HTML processing continues.

JavaScript that is handled by a client browser is called client-side JavaScript. Netscape Navigator created server-side JavaScript to be programmed as a CGI Script , such as Perl or ASP.

The History of JavaScript

 JavaScript was originally developed by Brendan Eich at Netscape sometime in 1995–1996 Back then, the language was called LiveScript. That was a great name for a new language and the story could have ended there However, in an unfortunate decision, the folks in marketing had their way, and the language was renamed to JavaScript Confusion soon ensued You see, Java was the exciting new language at the time, and someone decided to try to capitalize on Java’s popularity by using its name As a result, JavaScript found itself associated with the Java language This was a disadvantage for JavaScript, because Java, though popular in the sense that it was frequently used, was also unpopular because it had earned a fairly bad reputation—developers used Java in websites to present data or to add useless enhancements (such as annoying scrolling text) The user experience suffered because Java required a plug-in to load into the web browser, slowing down the browsing process and causing grief for visitors as well as accessibility problems. Only in recent years has JavaScript begun to separate from this negative Java association.

JavaScript is not a compiled language, which makes it look and feel like a language that lacks power. But programmers new to JavaScript soon came to realize its strengths and usefulness for both simulating and creating interactivity on the World Wide Web. Up until that realization, programmers developed many websites using only simple Hypertext Markup Language (HTML) and graphics that often lacked both visual appeal and the ability to interact with the site’s content.

Early JavaScript concentrated on client-side form validation and working with images on web pages to provide rudimentary, though helpful, interactivity and feedback to the visitor. When a visitor to a website filled in a form, JavaScript instantly validated the contents of the web form rather than make a roundtrip to the server. Especially in the days before broadband was pervasive, preventing the roundtrip to the server was a great way to help applications seem a little quicker and more responsive—and it still is.

JavaScript in a Browser

Client-side JavaScript

Client-side JavaScript is the most common form of the language. The script should be included in or referenced by an HTML document for the code to be interpreted by the browser. Suppose you load a web page with a JavaScript-generated banner. The browser begins interpreting the HTML code and then encounters a JavaScript script. If the script has no syntax errors, it is executed by the browser’s built-in interpreter. This is the most common method of implementing JavaScript because it is the easiest. Just write your scripts directly into the HTML and you are done.

The JavaScript client-side mechanism features many advantages over traditional CGI server-side scripts. For example, you might use JavaScript to check if the user has entered a valid e-mail address in a form field. The JavaScript function is executed when the user submits the form, and only if the entry is a valid e-mail address (includes an @) is the form transmitted to the server for further processing, database storage, and so on. In this case, the user does not need to wait until the form is submitted over the network to the server and back to the client, only to be informed that a required character is missing.

Browser Objects

JavaScript uses a built-in object model that is mostly based on the HTML content of the given web page. This object model, called the Document Object Model (or DOM), is used by many web technologies to access the content of HTML documents as well as the browser itself. The tight interaction between JavaScript and other browser objects (such as forms, browser windows, frames, and images) provides full control over various page elements and enables the programmer to create a link between “external” objects and “internal” ones. A classic example for such linking is a JavaScript script that invokes a Java applet from an HTML form. JavaScript exposes objects and properties related to the browser’s window, history list, status bar, frames, forms, links, and so forth. All of these are part of that Document Object Model. Furthermore, JavaScript can be used to trap user-initiated events such as button clicks, link navigation, and other actions that the user explicitly or implicitly initiates. You can create a distinct script for each event, enabling a smooth, logical interaction with the user.

What Can JavaScript Do?

JavaScript programs are used to detect and react to user-initiated events, such as a mouse going over a link or graphic. They can improve a Web site with navigational aids, scrolling messages and rollovers, dialog boxes, dynamic images, shopping carts, and so forth. JavaScript lets you control the appearance of the page as the document is being parsed. Without any network transmission, it lets you validate what the user has entered into a form before submitting the form to the server. It can test to see if the user has plug-ins and send them to another site to get the plug-ins if needed. It has string functions and supports regular expressions to check for valid e-mail addresses, social security numbers, credit card data, and the like. JavaScript serves as a programming language. Its core language describes such basic constructs as variables and data types, control loop, if/else statements, switch statements, functions, and objects. It is used for arithmetic calculations, manipulates the date and time, and works with arrays, strings, and objects. JavaScript also reads and writes cookie values, and dynamically creates HTML based on the cookie value.

Image Effects

It is very easy to create interesting image effects with JavaScript. Effects such as image rollovers are common on many web sites.

Games

You can create many interesting games with JavaScript. There are basically two types of games you can create via JavaScript:

* Static games

* Dynamic games

Static games are usually logical ones, and thus do not require much action or immediate responses. For example, you can play tic-tac-toe without any live action, because even reloading the entire page for each move does not interfere with the game itself (although it might be very boring). Static games do not require image replacements or animation. Dynamic games, on the other hand, require real-time action and animation. For example, a Tetris game requires that the blocks will fall down the screen smoothly without having to wait each time for the screen to be refreshed. Dynamic games are often resource intensive and therefore must be programmed with efficiency in mind. Small, simple games are usually embedded in existing web pages, whereas large, complex games are most often placed on a separate page.

Banners

JavaScript is a full scripting language, so you can create an infinite number of distinct scripts. Nevertheless, some implementations became more popular than others due to the fact that they are easy to use and create interesting effects. Banners are a classic example of such scripts. A banner is a script that displays text in the browser’s status bar or in a form’s text field. The most common banners are those that scroll and those that type in messages sequentially.

Form Validation

JavaScript is tightly connected to browser objects, including forms and their elements; therefore, it provides a great amount of control over forms. Form validation is one of the most common applications of JavaScript, A classic form-related script is one that validates a form’s fields before it is submitted and cancels the submission to the server if an error is found in one of the fields. The advantage of using JavaScript in this particular example is that client-side validation is much faster than validation via transmission to the server and back to the client. For example, suppose you have a form that accepts data from the user, including his or her e-mail address. Let’s assume that the e-mail address is essential, and that a string containing an “@” character is assumed to be a valid e-mail address. There are two options:

You can choose not to validate the entries. In that case, when the user clicks the Submit button, the form is submitted to the server where a CGI script checks if the e-mail address field is valid and then processes the form. If the e-mail address does not contain an “@” character, the submission is denied and the appropriate message is returned to the user.

You can choose to use client-side validation via JavaScript. In that case the validating script is loaded as plain text (JavaScript code embedded in the HTML code). When the user clicks the Submit button, a JavaScript function is triggered to validate the form. If the e-mail address seems valid, the form is submitted to the server; otherwise, an appropriate message is immediately displayed and the submission is canceled. Note that the user does not have to wait for the form to be submitted to the server and back to the client, only to receive a message informing him or her that the e-mail address field contains an invalid entry.

What JavaScript Is Not

JavaScript is not Java. Java was developed at Sun Microsystems. JavaScript was developed at Netscape. Java applications are independent of a Web page whereas JavaScript programs are embedded in a Web page and must be run in a browser window. Java is a strongly typed language with strict guidelines while JavaScript is loosely typed and flexible. Java data types must be declared. JavaScript types such as variables, parameters, and function return types do not have to be declared. Java programs are compiled. JavaScript programs are interpreted by a JavaScript engine that lives in the browser JavaScript is not HTML, but JavaScript code can be embedded in an HTML document and is contained within HTML tags. JavaScript has its own syntax rules and expects statements to be written in a certain way. JavaScript doesn't understand HTML, but it can contain HTML content within its statements. All of this will become clear as we proceed.

JavaScript is object based but not strictly object oriented because it does not support the traditional mechanism for inheritance and classes found in object-oriented programming languages, such as Java and C++. The terms private, protected, and public do not apply to JavaScript methods as with Java and C++.

JavaScript is not the only language that can be embedded in an application. VBScript, for example, developed by Microsoft, is similar to JavaScript, but is embedded in Microsoft's Internet Explorer.

Where to Put JavaScript

Client-side JavaScript programs are embedded in an HTML document between HTML head tags <head> and</head> or between the body tags <body> and </body>. Many developers prefer to put JavaScript code within the <head> tags. If you want text displayed at a specific spot in the document, you may want to place the JavaScript code within the <body> tags. Or you may have multiple scripts within a page, and place the JavaScript code within both the <head> and <body> tags. In either case, a JavaScript program starts with a<script> tag, and and ends with a </script> tag. And if the JavaScript code is going to be long and involved, or may be reused, it can be placed in an external file (ending in .js) and loaded into the page. When a document is sent to the browser, it reads each line of HTML code from top to bottom, and processes and displays it. As JavaScript code is encountered, it is read and executed by the JavaScript interpreter until it is finished, and then the parsing and rendering of the HTML continues until the end of the document is reached.

01.<html>
02.<head><title>First JavaScript Sample</title></head>
03.<body bgcolor="yellow" text="blue">
04.<script language = "JavaScript" type="text/javascript">
05.document.writeln("<h2>Welcome to the JavaScript World!</h1>");
06.</script>
07.<font size="+2">This is just plain old HTML stuff.</font>
08.</body>
09.</html>

EXPLANATION

1.      This is the starting tag for an HTML document.

2.      This is the HTML <head> tag. The <head> tags contain all the elements that don't belong in the body of the document, such as the <title> tags, and JavaScript tags.

3.      The <body> tag defines the background color and text color for the document.

4.      This <script> tag is the starting HTML tag for the JavaScript language. JavaScript instructions are placed between this tag and the closing </script> tag. JavaScript understands JavaScript instructions, not HTML.

The JavaScript writeln function is called for the document. The string enclosed in parentheses is passed to the JavaScript interpreter. If the JavaScript interpreter encounters HTML content, it sends that content to the HTML renderer and it is printed into the document on the browser. The normal HTML parsing and rendering resumes after the closing JavaScript tag is reached.

5.      This is the ending JavaScript tag.

6.      HTML tags and text continue in the body of the document.

7.      The body of the document ends here.

8.      This is the ending tag for the HTML document.


JavaScript Comments

JavaScript While

JavaScript For Loop 

JavaScript If Else

JavaScript Function

JavaScript Operators



This free website was made using Yola.

No HTML skills required. Build your website in minutes.

Go to www.yola.com and sign up today!

Make a free website with Yola