pTutorial

New way to learn Programming

XMLHttpRequest Object in Ajax

XMLHttpRequest Object

An object of XMLHttpRequest is used to communicate with a web server without submitting a form or reloading the new web page which means that the user need not wait until the server replies.

Syntax of XMLHttpRequest Object

For most of the Morden browser like google chrome firefox, Internet Explorer 7 +

var obj=new XMLHttpRequest();

For Internet Explorer 5 and 6

variable=new ActiveXObject("Microsoft.XMLHTTP");

Ajax with HTML


<!DOCTYPE html>
<html>
<head>
<script>
function Url()
{
var u;
if(window.XMLHttpRequest)
{
u=new XMLHttpRequest();
}
else
{
u=new ActiveXObject("Microsoft.XMLHTTP")
}
u.onreadystatechange=function ()
{
if(u.readyState==4 && u.status==200)
{
document.getElementById("kk").innerHTML=u.responseText;
}

}
u.open("GET","test.txt",true);
u.send();
}
</script>
</head>


<body>
<h1>Load The txt file using Ajax via button in the same page</h1>
<button type="button" onclick="Url()">Load Test File </button>
<hr>
<div id="kk"></div>
</body>
</html>

test.txt file

This is the text file Load via Ajax 

See Also


QUOTES OF THE DAY

The heart of melody can never be put down.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER