View Single Post
Old 02-14-2013, 07:59 AM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,468
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by Old Pedant View Post
As Felgall said.

One better way (and there are others, but this works for me) is:
Code:
function createRequestObject() {
    return   ( XMLHttpRequest != null )
           ? new XMLHttpRequest( ) 
           : new ActiveXObject("Microsoft.XMLHTTP");
}
Another seemingly popular way:
Code:
function createRequestObject() 
{
    try {
        return  new XMLHttpRequest();
    } catch ( ignored ) { }
    try {
        return new ActiveXObject("Microsoft.XMLHTTP");
   } catch ( oops ) {
        return null;
   }
}
Of course with IE6 being dead you can even drop the activeX reference completely and just use new XMLHttpRequest() since all the browsers people current use support it.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/

Last edited by felgall; 02-14-2013 at 08:02 AM..
felgall is offline   Reply With Quote