我使用以下代码,其中该函数被称为 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 之外的所有浏览器
最佳答案
它在 Netscape Navigator 中不起作用,因为这个(古老的)浏览器既不支持 XMLHttpRequest 对象,也不支持在旧版本 IE 中工作的 ActiveX 替代对象。
当 Navigator 的最后一个版本发布时,XMLHttpRequest 对象甚至还没有发明,而 ActiveX 替代方案仅适用于 IE。
如果您真的非常想让现代 Ajax 网站在像这样的古老浏览器上运行,您可能可以使用旧的“隐藏 iframe”技术来完成一些事情 hack,但这需要大量工作才能获得几乎为零的 yield ,并且为了支持浏览器,您仍然需要解决大量其他问题。
关于ajax - 为什么 AJAX 在 Netscape Navigator 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7672475/