naees
naees

Reputation: 21

How to create XMLHttpRequest

I just need the clear explanation of the below code for creating XMLHttpRequest.

var xhr = false;
if (window.XMLHttpRequest)
{
    xhr = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
    xhr = new ActiveXObject("Microsoft.XMLHTTP");
}

Upvotes: 2

Views: 1063

Answers (1)

ThiefMaster
ThiefMaster

Reputation: 318478

It tries to create a native XMLHttpRequest object and if that fails (ancient IE versions), it tries to use the XHR ActiveX object.

Note that it would be good to use e.g. jQuery for AJAX - it wraps it nicely, makes your code much more readable and saves you lots of work.

Upvotes: 3

Related Questions