为什么 AJAX 在 Netscape Navigator 中不起作用?

use*_*918 2 browser ajax function netscape navigator

我使用以下代码,其中该函数称为 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>
Run Code Online (Sandbox Code Playgroud)

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

Spu*_*ley 5

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

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

如果您真的非常想让现代 Ajax 网站在像这样的古老浏览器上工作,您也许可以使用旧的“隐藏 iframe”技术hack 来做一些事情,但这需要大量工作,几乎为零收益,为了支持浏览器,您仍然需要解决大量其他问题。