PrimitiveType

An Overview of JavaScript


JavaScript is a scripting language that is most commonly used to add interactivity to web pages. It is said to be object-based in that it has some support for object oriented programming. This article focuses on client-side JavaScript, although the language can be used in a number of server-side technologies, such as ASP.

Client-side JavaScript can be included in web pages in different ways. One way is to embed the JavaScript code directly in an HTML page. Another way is to state the location of a separate JavaScript file from within a web page. These two methods can be used within the same web page. JavaScript represents the web page & browser via the Document Object Model (DOM).

Some common usages of JavaScript that you may have encountered whilst surfing the web are:

  • Form validation. A pop-up box will appear when you attempt to submit a form, informing you that you need to make some changes to the data in the form fields.
  • Sophisticated dropdown menus similar to those found in desktop applications.
  • News scrollers, where the headlines scroll from left to right on a news website, much like on television news.

Recently JavaScript use has increased with the advent of AJAX, a combination of technologies that uses JavaScript and XML to provide a more seamless user experience for web applications. An example application is Google's GMail interface. This technology looks like it will feature prominently in the foreseeable future of the web.

Not all browsers support JavaScript, and in some cases users switch off support in browsers that do. As such, it is best practice to design websites so that they work with or without JavaScript, or so that they ask the user to switch it on or move to a supporting browser if their's is incompatible. As an example, a site shouldn't use JavaScript alone to do its validation on forms, as a user could turn JavaScript off and submit an invalid form. In this scenario, the best solution is to always validate forms on the server and include client-side JavaScript as a convenience to users whose broswers are using it.

Regrettably, writing JavaScript that works in all browsers is a difficult task as the language does not function in exactly the same way in all browsers. Aside from the expected updates to the language, which means some of its latest features only work in the more recent versions of browsers, there are differences across browser vendors. The classic example is Microsoft Internet Explorer versus Mozilla Firefox (and previously Netscape Navigator). In fact, Internet Explorer only supports its own version of JavaScript called JScript, which diverges from standard JavaScript in a number of areas. The discrepancies make writing and reading JavaScript code harder and more cumbersome.

JavaScript as it runs in a browser is restricted for security reasons. Despite this, it has been exploited by hackers using, for example, cross site scripting (XSS).

JavaScript is not closely related to the Java programming language (both languages descend in part from the C family of programming languages), but took its name to jump on the back of the popularity and hype surrounding Java in the mid-nineties. That aside, there are ways for JavaScript to communicate with a Java program.

An alternative to JavaScript for client-side web browser scripting is VBScript, a subset of the Visual Basic programming language developed by Microsoft. However, VBScript is less widely supported in browsers and as such is rarely used.