Every cutting edge program bolster the XMLHttpRequest. The XMLHttpRequest protest is utilized to trade information with a server. This implies it is conceivable to overhaul parts of a page, without distrubing the entire page. The following is the Syntax for creating an
XMLHttpRequest object
.
[xml]variable = new XMLHttpRequest();[/xml]
The legacy versions of
Internet Explorer
will utilize an
ActiveX Object
as follows.
[xml]variable = new ActiveXObject("Microsoft.XMLHTTP");[/xml]
To deal with all browsers, check if the program underpins the XMLHttpRequest protest. If it happen then create a XMLHttpRequest protest, if not, make an ActiveXObject as follows.
[xml]
<!DOCTYPE html>
<html>
<body bgcolor="skyblue">
<h1>The XMLHttpRequest Object</h1>
<p id="request">Let AJAX change this text.</p>
<button type="button" onclick="loadDoc()">Modify Content</button>
<script>
function loadDoc() {
var xhttp;
if (window.XMLHttpRequest) {
// code for modern browsers
xhttp = new XMLHttpRequest();
} else
{
// code for IE6, IE5
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
}
</script>
</body>
</html>>
[/xml]
The
id
property gives a one of a kind identifier to a component inside the record. It might be utilized by an a component to make a hyperlink to this specific component.
The
document is loaded
event is a pleasant snare for page initialization. All components are at place and client can create interfaces on them.
The
onreadystatechange
function will be put away and will be called naturally every time readyState property changes
Output: Now compile the code result will be as follows.