AJAX browsers
When creating an AJAX object, you should always keep in mind that an AJAX object cannot be created the same way for all browsers. An AJAX object is created one way for Internet Explorer and a different way for other browsers.
This lesson focuses on:
- Creating an AJAX object for various browsers
Creating an AJAX object for various browsers
You can create one set of code in Javascript that will dictate what kind of AJAX should object should be created based on what browser is being used. An AJAX object is created one way for Internet Explorer and a different way for other browsers. Internet Explorer uses an ActiveXObject, while other browsers use XMLHttpRequest.
To deal with this duality, we will use a try.....catch block in Javascript. To learn more about try.....catch blocks, read our Javascript errors lesson.
Example:
<script type="text/javascript">
function ajaxDataGet(){
var ajaxObject; //the variable that will store the object
try{
// Opera 8.0+, Firefox, Safari, Netscape 7
ajaxObject = new XMLHttpRequest();
}
catch (e){
// Internet Explorer
try{
ajaxObject = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
//if the above ActiveXObject did not work
try{
ajaxObject = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
//if AJAX is not supported at all
alert("Your browser does not support AJAX!! How is it back in 1998?");
return false;
}
}
}
}
</script>
In the above example, the function ajaxData() is used to set up an AJAX object. The variable ajaxObject is used to store the AJAX object. The rest of the code invloves a try.....catch block that will create the AJAX object depending on whether the user is using Internet Explorer or some other browser.
The try.....catch block says to first try to create an AJAX object using XMLHttpRequest(). If that does not work (meaning the user is using either Internet Explorer or a browser that does not support AJAX), create an AJAX object using new ActiveXObject("Msxml2.XMLHTTP"). If that does not work (meaning the user is using either Internet Explorer that does not support creating an AJAX object in this method or a browser that does not support AJAX), create an AJAX object using new ActiveXObject("Microsoft.XMLHTTP"), and if that does not work it can only mean that the user is using a browser that does not support AJAX and we issue the message "Your browser does not support AJAX!! How is it back in 1998?"
The above is the core code to create an AJAX object.
To reiterate, the logical progression it follows is as such:
- Create a function to set up an AJAX object
- Create a variable within that function to store the AJAX object
- Use a try.....catch block to try to crate an AJAX object using XML HttpRequest()
- If that does not work, create an AJAX object using new ActiveXObject("Msxml2.XMLHTTP")
- If that does not work, create an AJAX object using new ActiveXObject("Microsoft.XMLHTTP")
- If that does not work, issue a message informing the user that their browser does not support AJAX
You do not have to use the same exact code to create AJAX objects. But whatever code you use, the logical progression should be the same.




