Javascript
Last edited April 1, 2008
More by Ian Lewis »
Sections:

Howto Delete An Element From An Array

You can delete an item from an Array with the splice() method. Simply supply the index of the item you wish to delete and how many items you want to delete and the item(s) will be removed for you.

var myArray=[1,2,3,4,5,6,7,8,9,10];

myArray.splice(5,1);        //delete one item at the 5th index.
document.writeln(myArray);  // outputs: 1,2,3,4,5,7,8,9,10

Howto Easily Add Items To An Array

There are three ways to easily add items to an array. First you can use the Array.length property. Since Arrays start with index 0, then Array.length is always equal to the first empty index at the end of the Array.

var myArray = [];
myArray[0] = 'January';
myArray[1] = 'February';

document.writeln(myArray.length);          // Will output: 2

myArray[myArray.length] = 'March';         // Adds Item to end of Array

document.writeln(myArray.length);          // Will output: 3

document.writeln('0>'+myArray[0]+'<BR>');          // Will output: 0>January
document.writeln('1>'+myArray[1]+'<BR>');          // Will output: 1>February
document.writeln('2>'+myArray[2]+'<BR>');          // Will output: 2>March

You can add the .push() method of the Array. This will add the requested items to the end of the Array.

var myArray = [];
myArray[0] = 'January';
myArray[1] = 'February';

myArray.push('March');         // Adds Item to end of Array

document.writeln('0>'+myArray[0]+'<BR>');          // Will output: 0>January
document.writeln('1>'+myArray[1]+'<BR>');          // Will output: 1>February
document.writeln('2>'+myArray[2]+'<BR>');          // Will output: 2>March

You can use the .unshift() method to insert an item at the BEGINNING of the array!

var myArray = [];
myArray[0] = 'February';
myArray[1] = 'March';

myArray.unshift('January');         // Adds Item to beginning of Array

document.writeln('0>'+myArray[0]+'<BR>');          // Will output: 0>January
document.writeln('1>'+myArray[1]+'<BR>');          // Will output: 1>February
document.writeln('2>'+myArray[2]+'<BR>');          // Will output: 2>March
Object-Oriented Programming with JavaScript, Part I: Inheritance: Inheritance through Functions -
www.webreference.com/js/column79/3.html

Inheritance through Functions

Although JavaScript does not support an explicit inheritance operator, you can implement inheritance in other ways. There are two different ways to establish a hierarchy of classes in JavaScript. The first method to create an object as a subclass of another object, is to call the superclass constructor function inside the subclass object definition. Let's look at the following example:

function superClass() {

  this.bye = superBye;
  this.hello = superHello;
}

function subClass() {
  this.inheritFrom = superClass;
  this.inheritFrom();
  this.bye = subBye;
}

function superHello() {
  return "Hello from superClass";
}
  
function superBye() {
  return "Bye from superClass";
}

function subBye() {
  return "Bye from subClass";
}

Click here to invoke the following assignment and function that activate these objects:

function printSub() {
  var newClass = new subClass();
  alert(newClass.bye());
  alert(newClass.hello());
}

Convince yourself that it is working correctly. The methods bye() and hello() are first defined in superClass(). The method bye() is being overridden in subClass(). The first two lines of subClass() does the original inheritance between the two classes. You first define the inheritFrom method, and then you call it:

this.inheritFrom = superClass;
this.inheritFrom();

Let's take another example. Here is the definition of superclass() and subclass() (different from the above superClass() and subClass()):

function superclass() {
  this.info = alert("Defining the Superclass");
}

function subclass() {
  this.inheritFrom = superclass;
  this.inheritFrom();
  this.info = alert("Overriding the Superclass");
}

To activate the generation of subclass(), click here. This button calls the following function:

function createSubclass() {
  var newClass = new subclass();
}

Notice the alert boxes. They show that the info method is first defined in superclass() and then is being overridden in subclass()

Javascript Arrays Are Passed By Reference

Arrays are passed to functions by reference, or as a pointer to the original. This means anything you do to the Array inside the function affects the original.

var myArray = [ 'zero', 'one', 'two', 'three', 'four', 'five' ];

document.writeln(myArray[1]); // Will output: one

function passedByReference(refArray) {
   refArray[1] = 'changed';
}

passedByReference(myArray);

document.writeln(myArray[1]); // Will output: changed

Initializing An Array

You can initialize your array with pre-defined data…

var myArray = ['January', 'February', 'March'];
document.writeln('0>'+myArray[0]+'<BR>');                  // Will output: 0>January
document.writeln('1>'+myArray[1]+'<BR>');                  // Will output: 1>February
document.writeln('2>'+myArray[2]+'<BR>');                  // Will output: 2>March

You can inizialize your array with data after an empty array has been created…

var myArray = [];
myArray[0] = 'January';
myArray[1] = 'February';
myArray[2] = 'March';
document.writeln('0>'+myArray[0]+'<BR>');                  // Will output: 0>January
document.writeln('1>'+myArray[1]+'<BR>');                  // Will output: 1>February
document.writeln('2>'+myArray[2]+'<BR>');                  // Will output: 2>March

If you skip an element, the blank Array elements will be of type undefined

var myArray = [];
myArray[0] = 'January';
myArray[1] = 'February';
myArray[5] = 'March';
document.writeln('0>'+myArray[0]+'<BR>');                  // Will output: 0>January
document.writeln('1>'+myArray[1]+'<BR>');                  // Will output: 1>February
document.writeln('2>'+myArray[2]+'<BR>');                  // Will output: 2>undefined
document.writeln('3>'+myArray[3]+'<BR>');                  // Will output: 3>undefined
document.writeln('4>'+myArray[4]+'<BR>');                  // Will output: 4>undefined
document.writeln('5>'+myArray[5]+'<BR>');                  // Will output: 5>March

Javascript Arrays Are Assigned By Reference

Assigning an Array to a new variable creates a pointer to the original Array. For instance…

var myArray = [ 'zero', 'one', 'two', 'three', 'four', 'five' ];
var newArray= myArray;

newArray[1] = 'changed';

document.writeln(myArray[1]); // Will output: changed

Array Methods Reference

Since Javascript Arrays are modified objects, each and every Array you create has a few core methods. What's really interesting is that some of these methods implement basic data structures you'd normally have to write yourself such as stacks (push, pop) and queues (shift, unshift).

MethodIE VersionMozilla VersionNotes
concat4.04.0Joins multiple Arrays
every*FF 1.5Calls a function for every element of the array until false is returned.
filter*FF 1.5Creates an array with each element which evaluates true in the function provided.
forEach*FF 1.5Executes a specified function on each element of an Array
join3.03.0Joins all the Array elements together into a string.
indexOf*FF 1.5Searches the Array for specific elements.
lastIndexOf*FF 1.5Returns the last item in the Array which matches the search critera.
map*FF 1.5Creates a new array with the result of calling the specified function on each element of the Array.
pop5.54.0Returns the last item in the Array and removes it from the Array.
push5.54.0Adds the item to the end of the Array.
reverse3.03.0Reverses the Array so the last item becomes the first and vice-versa.
shift5.54.0Returns the first item in the Array and removes it from the Array.
slice4.04.0Returns a new array from the specified index and length.
some*FF 1.5Passes each element through the supplied function until true is returned.
sort3.03.0Sorts the array alphabetically or by the supplied function.
splice5.54.0Deletes the specified index(es) from the Array.
toSource---FF 1.5Returns the source code of the array.
toString3.03.0Returns the Array as a string.
unshift5.54.0Inserts the item(s) to the beginning of the Array.
valueOf3.03.0Like toString, returns the Array as a string.

* Prototype functions are available to make this method available to Internet Explorer and older browsers.

Ajax | tutorial, cross-browser Ajax || HTMLSource ]
www.yourhtmlsource.com/javascript/ajax.html

What is “Ajax”?

Ajax is actually a family of technologies that have been available for years. The means to make requests to the server using only JavaScript were built into Internet Explorer 5.5, but the possibilities of the technology were overlooked. It was only in 2005 that the techniques were rediscovered and used, notably to excellent effect in Google’s » GMail web application.

The term Ajax, which stands for “Asynchronous JavaScript and XML”, was first coined by Jesse James Garrett in his somewhat infamous article, » Ajax: A New Approach to Web Applications.

So let’s take each of those parts in isolation. Ajax is:

Asynchronous
This means that when you send a request, you wait for the response to come back, but are free to do other things while you wait. The response probably won’t come back immediately, so you set up a function that will wait for the response to be sent back by the server, and react to it once that happens.
JavaScript
JavaScript is used to make a request to the server. Once the response is returned by the server, you will generally use some more JavaScript to modify the current page’s document object model in some way to show the user that the submission went through successfully.
XML
The data that you receive back from the server will often be packaged up as a snippet of XML, so that it can be easily processed with JavaScript. This data can be anything you want, and as long as you want.

There’s nothing really new about what is happening here. We’re requesting a file (which will often be a server-side script, coded in something like PHP), and receiving a page as the response. This is how the web works already — the only difference is that now we can make these requests from JavaScript.

Cross-browser Ajax

Unfortunately Ajax is supported slightly differently in IE than it is Safari, Opera and Mozilla-based browsers like Firefox. This leaves us with two possible routes: using code branching to send the right code to each browser based on which model they support, or using a JavaScript library that wraps up the Ajax code into a single object, and means you don’t have to worry about browser incompatibilities.

We’re going for the latter option, and will be using a JavaScript library. There are dozens of them in existence, each with their own boons and vices. Popular examples include prototype, Dojo, and the Yahoo UI library. For the duration of this tutorial, we’re going to be using a very useful library called » Sarissa. Sarissa contains methods that will create the request for us, and also methods that help with processing the XML that we receive back in the response. This means we don’t have to mess with the intricacies of Ajax, and allows our code to be quite elegant.

Building a Request

So, first » download the Sarissa JavaScript source code file (called “sarissa.js”) and then upload it with the rest of your files. You’ll need to show your pages where the library is, so add this line to the top of any pages that need access to Ajax:

<script type="text/javascript" src="sarissa.js"></script>

First of all, the project we’re going to work on throughout this tutorial is to make form submissions for a survey less painful. You can get a glimpse of what we’re aiming for in this example page. The user will be presented with a set of options, be asked to choose one, and press submit. Instead of having to reload the whole page to submit the form, which might turn some users off participating in the survey, we will instead submit the form with Ajax, and update the page when the request succeeds.

I’ll go through the four steps required to get this working, and we’ll get onto the full code later. First we’ll begin to construct our request. The specific method we use to do this is called “XML HTTP Request.” Through Sarissa, we create a cross-browser XML HTTP Request object:

var xmlhttp =  new XMLHttpRequest();

That code there will take care of the browser compatibility issues, and leave you with an object that will work in whatever the user’s browser can support. Next we specify the page that we’re requesting:

xmlhttp.open('POST', 'rating-submit.php', true);

The first argument to this function can be set to either 'GET' or 'POST'. In this instance, because we’re sending some data to the server, we use the ‘POST’ method. If we were only pulling additional data from the server, we could use the ‘GET’ method safely. This distinction is important to get right. Only use ‘GET’ if you are not changing anything on the server, just retrieving some data. Otherwise you should always use ‘POST’. Keep the names capitalised.

Obviously you will need to change that URL to the filename of the script you’re using on your own server. The third argument controls whether the request is asynchronous or synchronous. If set to false, the call is made synchronous and the user’s browser will actually lock up until the response is received, which is not what we want. You will almost always leave this as true.

The next step is to set up the function that will wait to be called until a response is returned by the server. This function is responsible for doing something to show the user that the submission has succeeded. Usually, adding a small message to the page is enough. The “callback function,” as it is called, always follows the same structure, which looks like this:

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4) {
        // Your callback code goes here
    }
}

As a request is made, it passes through various states. We check the object’s readyState property here to see if it has made it to stage 4, which means that the response is complete.

The last step now that this is all set up is to actually send the request:

xmlhttp.send(null);

This will send the request and then return immediately. It then waits for the response to enter readyState 4. When that happens, your callback function is called, and whatever data was sent back in the response (if any) is available in the xmlhttp.responseXML variable. And that’s all there is to it.

Building the Script

So now that we have a skeleton for the script, we can start filling it out properly. This script will use a number of functions from the very useful util-functions.js file, which is a collection of functions I put together that you are welcome to download and use in your own projects.

First, we’re going to loop through the page once it loads and add an event handler to any form with the class “ajaxify.” The event handler will be fired when the form is submitted:

addEvent(window, 'load', init, false);

function init() {
    if (!Sarissa || !document.getElementsByTagName) return;
    
    var formElements = document.getElementsByTagName('form');
    for (var i = 0; i < formElements.length; i++) {
        if (formElements[i].className.match(/\bajaxify\b/)) {
            addEvent(formElements[i], 'submit', submitRating, false);
        }
    }
}

So here we’re first checking if the user’s browser has correctly loaded the Sarissa object, and is also able to do the document.getElementsByTagName method. If this doesn’t work out, we return early. Then we’re looping through all the forms on the page and adding an event handler to any that have a class name that matches the string “ajaxify.” Next we have to actually write that submitRating function. It looks like this:

function submitRating(e) {
    /* Cancel the submit event, and find out which form was submitted */
    knackerEvent(e);
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    /* Set up the request */
    var xmlhttp =  new XMLHttpRequest();
    xmlhttp.open('POST', 'rating-submit.php', true);
    
    /* The callback function */
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200)
                addRatingFeedback(xmlhttp.responseXML, target);
            else
                target.submit();
        }
    }
    
    /* Send the POST request */
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send('rating=' + target.elements['rating'].value);
    …
}

The Callback Function

Let’s look at the callback function in a little more depth. We’ve added a new check in there.

if (xmlhttp.readyState == 4) {
    if (xmlhttp.status == 200)
        addRatingFeedback(xmlhttp.responseXML, target);
    else
        target.submit();
}

Just because we’ve received a response back doesn’t mean that everything went according to plan. When the response is sent back by the server, it is always sent with a status code, of which there are many. The response code 200 (known as “Ok”) means that the response went through without a hitch, which is obviously what we want to hear. If xmlhttp.status is set to 200, then we can go ahead and add some feedback to the page.

Otherwise, in the case of a timeout or if the script returned a 404 error, we need to have a backup plan. Here, I’ve decided to simply submit the form normally. This illustrates an important point: even with all of this Ajax goodness, you still need to have a script on the server that will deal with normal form submits from users with browsers that don’t support Ajax, or just for those times when something goes wrong.

Sending the POST Request

Sending a POST request, as opposed to a GET request, requires an extra step. We need to set a special attribute of the request, so that it accepts some data from the form. We then need to extract the relevant data from the form and include this in the send() call.

xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send('rating=' + target.elements['rating'].value);

As you can see, we’re no longer simply calling send(null). We send the data from the form in the format “name=value&name=value...”. So here we’re working with the select called “rating”, and sending its value, which will end up looking something like “rating=3”.

The Server-side Script

Everyone’s server-side script is going to be different, but for the sake of completeness, here’s what the one in our example looks like:

<?php
  header('Content-type: text/xml');
  
  $rating = $_POST['rating'];
  
  function record_rating($rating) {
    // Add data to database here
    return 1;
  }
?>
<xmlresponse>
    <rating><?= $rating ?></rating>
    <result><?= record_rating($rating) ?></result>
</xmlresponse>

This simply sends back a small XML document. You can make up whatever tags you want. The “record_rating” method I’ve glossed over here will save some data on the server, and then return a 1 if the operation was successful, but a 0 if the operation failed. This way you can check the contents of the <result> tag and add feedback appropriately.

Adding Feedback

The final step is to check the response and add feedback to the page appropriately. Again, this will be different for everyone’s implementation. The XML that was sent back by your server-side script is available in the xmlhttp.responseXML property. You can treat this just as you would a HTML document, which means that methods calls like the one below will yield the contents of certain elements in the XML:

xmlhttp.responseXML.getElementsByTagName('result')[0].firstChild.data;

That code snippet above will return the contents of the first <result> element in the response (i.e. the value you should check to make sure the server-side script executed correctly). The feedback you give the user can range from popping up an alert() to rearranging the page using DOM calls. In the example page you can see that I add new text to the page, restyle the form and then disable the form inputs so that the user doesn’t submit the same form twice.

So there you have it, an Ajax-ified page from start to finish. Ajax has lots of useful applications. Have fun with it.

Prototype

Prototype JavaScript framework: Easy Ajax and DOM manipulation for dynamic web applications
www.prototypejs.org/

Prototype is a JavaScript Framework that aims to ease development of dynamic web applications.

Featuring a unique, easy-to-use toolkit for class-driven development and the nicest Ajax library around, Prototype is quickly becoming the codebase of choice for web application developers everywhere.

The content on this page is provided by a Google Notebook user, and Google assumes no responsibility for this content.