Ajax Browser Support
All Browsers (opera, Safari, firefox) can support XMLHttpRequest object but IE5 and IE6 uses an ActiveXObject. XMLHttpRequest object is used to exchange data with a server behind the scenes. This means that it is possible to update parts of a web page, without refreshing the whole page.
NOTE: When we are saying that browser does not support AJAX it simply means that browser does not support creation of Javascript object XMLHttpRequest object.
Create an XMLHttpRequest Object
All modern browsers (IE7+, Firefox, Chrome, Safari, and Opera) have a built-in XMLHttpRequest object.
xmlhttp=new XMLHttpRequest();
but old version of Internet explorer (IE5, IE6) uses an Activex Object
We can write code of browser support with 2 ways
Now we will write ajax code for support browsers.
<body>
<script type="text/javascript">
function ajaxFunction(){
var ajaxReq; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxReq = new XMLHttpRequest();
}catch (e){
try{
// Internet Explorer Browsers
ajaxReq = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e) {
try{
// Internet Explorer Browsers
ajaxReq = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
// if not support
alert("Your browser dont suppor Ajax!");
return false;
}
}
}
}
</script>
</body>
</html>
Example explained
1. Create a variable named ajaxReq to hold the XMLHttpRequest object.
2. Try to create the XMLHttpRequest object with ajaxReq=new XMLHttpRequest().
3. If that fails, try ajaxReq=new ActiveXObject("Microsoft.XMLHTTP"). This is for IE6 and IE5.
4. If that fails too, the user has a very outdated browser, and will get an alert stating that the browser doesn't support XMLHTTP.