How to create an XMLHttpRequest Object?

How to create an XMLHttpRequest Object?

 We can create an XMLHttpRequest Object by simply using an one line code of JavaScript.

When we use on all browsers after come IE7.write this line to create an XMLHttpRequest Object.

var user=new XMLHttpRequest()


When we work with IE 5 and 6.

var user=new ActiveXObject(\"Microsoft.XMLHTTP\")


Example:

<script type=\"text/javascript\">

var user;

function loadXMLDoc(url)

{

   user=null;

   if (window.XMLHttpRequest)

   {    // for browsers above IE 6

      user=new XMLHttpRequest();

   }

   else if (window.ActiveXObject)

   {    // code for browser IE5 and IE6

      user=new ActiveXObject(\"Microsoft.XMLHTTP\");

   }

   if (user!=null)

   {

      user.onreadystatechange=state_Change;

      user.open(\"GET\",url,true);

      user.send(null);

   }

   else

   {

      alert(\"Your browser unable to support XMLHTTP.\");

   }

}


function state_Change()

{

   if (user.readyState==4)

   {        // 4 = \"loaded\"

      if (user.status==200)

      {     // 200 = OK

            // ***write code***

      }

      else

      {

         alert(\"Problem retrieving XML data\");

      }

   }

}

</script>


By:Vivek
Date:2015-01-09 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related XML Subjective Links

XML Subjective interview questions and answers for experienced and fresher