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

时间:2011-10-06 09:30:41

标签: ajax function browser navigator netscape

我正在使用以下代码,其中函数被调用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导航器之外的所有浏览器

1 个答案:

答案 0 :(得分:4)

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

当Navigator的最新版本发布时,甚至没有发明XMLHttpRequest对象,而且ActiveX替代方案只能用于IE。

如果你真的很想让一个现代的Ajax网站在这样的古老浏览器上工作,那么可能能够使用旧的“隐藏的iframe”技术做一些事情黑客攻击,但实际上零增益的工作量很大,为了支持浏览器,你仍然需要解决许多其他问题。