1. Technology

What is JavaScript Used For?

By

Surfing the internet
Henrik Jonsson/E+/Getty Images

There are a number of different places where JavaScript can be used but the most common place to use it is in a web page. In fact for most people using JavaScript, in a web page is the only place where they use it.

Let us consider web pages and just what purpose that JavaScript serves within the page.

Properly constructed web pages are built using up to three different languages.

The first requirement of a web page is to define the content of the web page. This is done using a markup language that defines what each of the component parts of the content are. The language that is normally used to markup the content is HTML although XHTML can also be used if you do not require the pages to work in Internet Explorer.

The HTML defines what the content is. When written properly no attempt is made to define how that content is supposed to look. After all the content will need to look different depending on what device is being used to access it. Mobile devices generally have smaller screens than computers. Printed copies of the content will have a fixed width and may not require all the navigation be included. For people listening to the page it will be how the page is read rather than how it looks that needs to be defined.

The appearance of a web page is defined using CSS which has the ability to specify which media the specific commands are to apply to so as to be able to have the content formatted appropriately for whatever device the page is being accessed with.

Using just these two languages you can create static web pages that will be accessible regardless of which device is used to access the page. These static pages can interact with your visitor through the use of forms. Once a form is filled out and submitted a request is sent back to the server where anew static web page is constructed and eventually downloaded into the browser.

The big disadavantage of web pages like this is that the only way that your visitor has of interacting with the page is by filling out the form and waiting for a new page to load.

The purpose of JavaScript is to solve this problem

It does this by converting your static page into one that can interact with your visitors without them needing to wait for a new page to load every time they make a request. JavaScript adds behaviour to the web page where the web page is capable of responding to actions by your visitors without needing to load a new web page in order to process their request.

No longer does your visitor need to fill out an entire form and submit it inorder to be told that they made a typo in the first field and need to enter it all again. With JavaScript you can validate each of the fields as they enter it and provide immediate feedback when they make a typo.

JavaScript also allows your page to be interactive in other ways that do not involve forms at all. You can add animations into the page which either attract attention to a specific part of the page or which make the page easier to use.You can provide responses within the web page to various actions that your visitor takes so as to avoid the need to load new web pages to respond. You can even have the JavaScript load new images, objects, or scripts into the web page without needing to reload the entire page. There is even a way for JavaScript to pass requests back to the server and handle responses from the server without the need for loading new pages.

Incorporating JavaScript into a web page allows you to improve your visitor's experience of the web page by converting it from a static page into one that can interact with them. One important thing to remember though is that not everyone visiting your page will have JavaScript and so your page will still need to work for those who don't have JavaScript. You use JavaScript to make your page work better for those who have it.

  1. About.com
  2. Technology
  3. JavaScript
  4. Reference
  5. What is JavaScript Used For?

©2014 About.com. All rights reserved.