COM271, Week 7

Introduction to Javascript

Syllabus | Table of Pages | Assignments | References and Useful Links

Client-side programming with JavaScript

With HTML and CSS, a developer can structure and style a web page. By adding JavaScript, you can add the flexibility of a universally supported programming language which works in all browsers. With JavaScript, you can write programs to have the browser do a number of things, including:

JavaScript works well with both html and css. It is written as part of a web page, and downloaded to the client's browser, where it provides a dynamic capability to respond to user mouse clicks and mouse movements. There is a rich array of things that JavaScript can be used for; indeed, JavaScript is the basis for ActionScript, which allows Adobe Flash pages to work (something beyond our present scope).

We'll limit our exposure to JavaScript to an outline of some of its basic characteristics and functions. Our main focus will be to acquaint you with how JavaScript is used to validate input forms before they are sent to the server. We will also look at a couple of ways to manipulate both CSS and HTML.

DHTML: The combination of Javascript with HTML and CSS is sometimes called DHTML, where the D stands for Dynamic. DHTML is dynamic in the sense that page elements can be changed. Page content (text and images) remain static, however. Truly dynamic web pages require the use of database to drive web content, the subject of COM372, Dynamic Web Design and Programming, a separate course involving PHP (server programming language) and MySQL, a database.

Here, we'll start with a few basics, and gradually work up to doing useful things. Our use of DHTML will include providing feedback to users about what they enter on an html form: was something entered into each required field; were the right kinds of things (numbers, letters) entered; and do the entries follow expected patterns, such as having the right pattern for a phone number or an email address? We will also provide visual feedback for incorrectly filled forms, as well as text to indicate errors, all constructed in a dynamic fashion using JavaScripts interpretted by the client browser.

Adding JavaScript to HTML: We won't be able to do anything until we first learn to write JavaScript into our web pages. Let's start with that. There are 4 ways to incorporate JavaScript into a web page:

  1. With an HTML event handler, e.g., onclick
  2. Via a pseudo-URL javascript: syntax referenced by a link (e.g., <a href="javascript:...)
  3. Code with a <script> element (uses attribute type="text/javascript")
    <script type="text/javascript"></script>
    These may be used as many times as desired, placed anywhere in the document.
  4. Code linked via src attribute of <script> element

We will return to JavaScript written as functions into the page's <script> element or from externally linked JavaScript files. Here are two simple ways to begin writing simple scripts on the page. Both rely on the browser having the ability to react to events:

Event handlers: JavaScript allows the browser to respond to things the user does with the mouse or keyboard, including placement of the cursor over page elements. This is supported by all contemporary browsers. Events can be bound to most elements, as we'll show next. Events are the familiar things you can do with a mouse. Each event is coupled with a response; together they are called event handlers. The names of events suggest the things users can do or things that happen because a user has taken some action:

  • onblur
  • ondblclick
  • onkeypress
  • onmousedown
  • onmouseover
  • onselect
  • onchange
  • onfocus
  • onkeyup
  • onmousemove
  • onmouseup
  • onsubmit
  • onclick
  • onkeydown
  • onload
  • onmouseout
  • onreset
  • onunload

Here is a simple link, using the onclick event handler to display a simple box with a message.

Mouse-over here.

<a onmouseover="alert('You just moused-over me! JavaScript can recognize mouse events!');">Mouse-over here</a>

Javascript Pseudo-URL: A second way of adding a small bit of JavaScript to a page is to replace the link address with a short script (one or more lines of JavaScript) preceded by "javascript:". (This is equivalent to inserting a small bit of CSS into an element using the style attribute.) It is used sparingly.

Click here!

<a href="javascript: alert('You clicked! Yes...?');">Click here!</a>

Hiding JavaScript from really old browsers or warning that it is turned off

While JavaScript works in all contemporary browsers, users do have an option to turn it off; they sometimes do this out of fear that JavaScript will expose their computer to viruses (mistaken). The <noscript> element will hide code from javascript-supporting (and enabled) browsers; use it to provide a warning message:
<noscript>
<p>This page makes use of client-side JavaScript for various page effects. If you are seeing this message, your browser does not support javascript, or—most likely—you have turned off JavaScript in your browser. JavaScript is safe, and it provides essential programming capabilities for web pages. Please upgrade or turn on JavaScript!</p>
</noscript>

JavaScript Standards

JavaScript has nothing to do with the general-purpose computing language, Java. JavaScript was introduced originally by Netscape as "LiveScript," in 1995, primarily for form validation. Syntactically, JavaScript more closely resembles C, Perl, and Python, than Java. During the browser-war years (mid 1990's), Netscape introduced JavaScript with Netscape 2, and Microsoft countered with JScript in IE 3.0. The closest thing to a current standard is the JavaScript recommendations of the standards organisation, Ecma International, which was previously the European Computer Manufacturers Association (ECMA). The current standard is ECMA-262 (1999). An XML extension to ECMA-262 (and to ActionScript, which is JavaScript used in Flash), is Standard ECMA-357 ): This is referred to as E4X.

Standards

ECMA-262 (http://www.ecma-international.org/publications/standards/Ecma-262.htm)

E4X (http://www.ecma-international.org/publications/standards/Ecma-357.htm