2

我正在使用以下代码,其中该函数被称为 onclick:

<html>
<head>
<script type="text/javascript">
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","ajax_info.txt",true);
    xmlhttp.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>

它适用于除 netscape navigator 之外的所有浏览器

4

1 回答 1

5

它在 Netscape Navigator 中不起作用,因为这个(古老的)浏览器既不支持 XMLHttpRequest 对象,也不支持在旧版本 IE 中工作的 ActiveX 替代方案。

当 Navigator 的最后一个版本发布时,甚至还没有发明 XMLHttpRequest 对象,而 ActiveX 替代方案仅适用于 IE。

如果你真的不顾一切想让现代 Ajax 网站在这样的古老浏览器上运行,你也许可以使用旧的“隐藏 iframe”技术hack 做一些事情,但要获得几乎为零的收益,这将是很多工作,为了支持浏览器,您仍然需要解决许多其他问题。

于 2011-10-06T10:44:39.477 回答