AJAX Chinese Re...LOGIN
AJAX Chinese Reference Manual
author:php.cn  update time:2022-04-12 16:00:57

XHR create object



XMLHttpRequest is the foundation of AJAX.


XMLHttpRequest Object

The XMLHttpRequest object is supported by all modern browsers (IE5 and IE6 use ActiveXObject).

XMLHttpRequest is used to exchange data with the server in the background. This means that parts of a web page can be updated without reloading the entire page.


Create XMLHttpRequest object

All modern browsers (IE7+, Firefox, Chrome, Safari and Opera) have built-in XMLHttpRequest objects.

Syntax for creating an XMLHttpRequest object:

variable=new XMLHttpRequest();

Old version of Internet Explorer (IE5 and IE6) use ActiveX objects:

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

To cope with all modern browsers , including IE5 and IE6, please check whether the browser supports the XMLHttpRequest object. If supported, creates an XMLHttpRequest object. If it is not supported, create an ActiveXObject::

Instance

<html><!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","/try/ajax/ajax_info.txt",true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>使用 AJAX 修改该文本内容</h2></div>
<button type="button" onclick="loadXMLDoc()">修改内容</button>

</body>
</html>

Run Instance»

Click the "Run Instance" button View online examples

In the next chapter, you will learn about sending server requests.

php.cn