Thread: Ajax
View Single Post
  #8  
Old 05-04-2009, 06:07 PM
welcomewiki welcomewiki is offline
Member
 
Join Date: Dec 2008
Location: India
Posts: 80,538
A Better Example?

Some programmers will prefer to use the newest and fastest version of the XMLHttpRequest object.



The example below tries to load Microsoft's latest version "Msxml2.XMLHTTP", available in Internet Explorer 6, before it falls back to "Microsoft.XMLHTTP", available in Internet Explorer 5.5 and later.







function GetXmlHttpObject()
{
var xmlHttp=null; try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}






Example above explained:
  1. First create a variable XMLHttp to use as your XMLHttpRequest object. Set the value to null.
  2. Try to create the object according to web standards (Mozilla, Opera and Safari):XMLHttp=new XMLHttpRequest()
  3. Try to create the object the Microsoft way, available in Internet Explorer 6 and later:XMLHttp=new ActiveXObject("Msxml2.XMLHTTP")
  4. If this catches an error, try the older (Internet Explorer 5.5) way: XMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
Reply With Quote