刚开始写AJAX代码的时候,直接参照的是AJAX基础教程一书中的代码(该书真的很不错,是AJAX入门的经典教材,是图灵出版社的。计算机方面的书籍,我最信任的就是O'R和图灵的)。
该书的创建XMLHttpRequest对象的代码如下:
01 |
var xmlHttp;
|
02 |
|
03 |
function createXMLHttpRequest()
|
04 |
{ |
05 |
if (window.ActiveXObject)
|
06 |
{
|
07 |
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
|
08 |
}
|
09 |
else if (window.XMLHttpRequest)
|
10 |
{
|
11 |
xmlHttp = new XMLHttpRequest();
|
12 |
}
|
13 |
} |
在一般情况下,该代码的使用不会带来任何问题。
如:
01 |
function test1()
|
02 |
{ |
03 |
createXMLHttpRequest();
|
04 |
xmlHttp.onreadystatechange = handleStateChange1;
|
05 |
url = "test.php?ts=" + new Date().getTime();
|
06 |
xmlHttp.open("GET", url, true);
|
07 |
xmlHttp.send(null);
|
08 |
} |
09 |
|
10 |
function test2()
|
11 |
|