Home arrow AJAX & Prototype arrow Ajax Features Needed to Build Active Client Pages

Ajax Features Needed to Build Active Client Pages

This is the second part of my four-part series, Active Client Pages Ajax Approach. In this part of the series, we continue our discussion of the Ajax features we will need to use with ACP, and then we look at the principles of the Ajax approach.

TABLE OF CONTENTS:
  1. Ajax Features Needed to Build Active Client Pages
  2. More About the XMLHttpRequest Object
  3. The responseText Property
  4. Principles of the Ajax Approach
By: Chrysanthus Forcha
Rating: starstarstarstarstar / 4
October 14, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The XMLHttpRequest Object

By using the XMLHttpRequest object, a web developer can update a page with data from the server after the page has loaded!

The XMLHttpRequest object is supported in Internet Explorer 5.0+, Safari 1.2, Mozilla 1.0 / Firefox, Opera 8+, and Netscape 7.

Ajax Browsers

The keystone of AJAX is the XMLHttpRequest object. Different browsers use different methods to create the XMLHttpRequest object. Internet Explorer uses an ActiveXObject, while other browsers uses the built-in JavaScript object called XMLHttpRequest.

All Ajax requests in JavaScript begin by making a call to the XMLHttpRequest constructor function:

new XMLHttpRequest() //IE7, Firefox, Safari etc;

new ActiveXObject("Msxml2.XMLHTTP") //newer versions of IE5+;

new ActiveXObject("Microsoft.XMLHTTP") //older versions of IE5+;

new XDomainRequest() //IE8+ only. A more "secure", versatile alternative to IE7's XMLHttpRequest() object.

In IE6 and below, the XMLHttpRequest() is not supported, but instead relies on the proprietary ActiveXObject for Ajax requests. To create this object, and deal with different browsers, we are going to use a "try and catch" statement. This is a typical way to address the problem (the explanation is given below):


<html>

<body>

<script type="text/javascript">function ajaxFunction()

{

var xmlHttp;

try

{

// Firefox, Opera 8.0+, Safari

xmlHttp=new XMLHttpRequest();

}

catch (e)

{

// Internet Explorer

try

{

xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

}

catch (e)

{

try

{

xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

}

catch (e)

{

alert("Your browser does not support AJAX!");

}

}

}

}

</script>

</body>

</html>


Explanation

In this illustration, the function you write that handles Ajax request is called ajaxFunction.

First we need to create a variable xmlHttp to hold the XMLHttpRequest object in the function.

Then we try to create the object with XMLHttp=new XMLHttpRequest(). This is for Firefox, Opera, and Safari browsers. If that fails, we try xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"), which is for Internet Explorer 6.0+. If that also fails, we try xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"), which is for Internet Explorer 5.5+.

If none of the three methods work, the user has a very outdated browser, and he or she will get an alert stating that the browser doesn't support AJAX.

Note: The browser-specific code above is long and quite complex. However, you can use this code every time you need to create an XMLHttpRequest object. The code above is compatible with all of the popular browsers: Internet Explorer, Opera, Firefox, and Safari.



 
 
>>> More AJAX & Prototype Articles          >>> More By Chrysanthus Forcha
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

AJAX & PROTOTYPE ARTICLES

- Best AJAX Tutorials for Forms
- The Best AJAX Tutorials
- 8 Great Ajax Tutorials
- Using Ajax and jQuery
- Using Ajax and jQuery with HTML Forms
- Ajax.org Offers Cloud9 IDE for JavaScript
- Java Technologies Provider ICEsoft Releases ...
- Using Recaptcha in AJAX Prototype Framework ...
- Google's Closure Compiler Service API: Addit...
- Installing Google Web Toolkit: Introducing t...
- Google's Closure Compiler Service API: Displ...
- Google's Closure Compiler Service API: Deliv...
- Google's Closure Compiler Service API: the A...
- Google's Closure Compiler Service API: the S...
- Google's Closure Compiler Service API: Optim...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: