AJAX - Browser Support

 

All the available browsers cannot support AJAX. Here is a list of major browsers that support AJAX.

When you write your next application, do consider the browsers that do not support AJAX.

NOTE − When we say that a browser does not support AJAX, it simply means that the browser does not support the creation of Javascript object – XMLHttpRequest object.

Writing Browser Specific Code

The simplest way to make your source code compatible with a browser is to use try...catch blocks in your JavaScript.

<html>
<body>
<scriptlanguage="javascript"type="text/javascript">
<!--
//Browser Support Code
functionajaxFunction(){
varajaxRequest;// The variable that makes Ajax possible!
 
try{
// Opera 8.0+, Firefox, Safari 
ajaxRequest=newXMLHttpRequest();
}catch(e){
 
// Internet Explorer Browsers
try{
ajaxRequest=newActiveXObject("Msxml2.XMLHTTP");
}catch(e){
 
try{
ajaxRequest=newActiveXObject("Microsoft.XMLHTTP");
}catch(e){
 
// Something went wrong
alert("Your browser broke!");
returnfalse;
}
}
}
}
//-->
</script>
 
<formname='myForm'>
         Name: <inputtype='text'name='username'/><br/>
         Time: <inputtype='text'name='time'/>
</form>
 
</body>
</html>

In the above JavaScript code, we try three times to make our XMLHttpRequest object. Our first attempt −

It is for Opera 8.0+, Firefox, and Safari browsers. If it fails, we try two more times to make the correct object for an Internet Explorer browser with −

If it doesn't work, then we can use a very outdated browser that doesn't support XMLHttpRequest, which also means it doesn't support AJAX.

Most likely though, our variable ajaxRequest will now be set to whatever XMLHttpRequest standard the browser uses and we can start sending data to the server. The step-wise AJAX workflow is explained in the next chapter.