为什么 AJAX 在 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>
它适用于除 netscape navigator 之外的所有浏览器
I am using the following code in which the function is called 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>
It works in all the browser except netscape navigator
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它在 Netscape Navigator 中不起作用,因为这个(古老的)浏览器既不支持 XMLHttpRequest 对象,也不支持在旧版本 IE 中工作的 ActiveX 替代对象。
当 Navigator 的最后一个版本发布时,XMLHttpRequest 对象甚至还没有发明,而 ActiveX 替代方案仅适用于 IE。
如果您真的非常想让现代 Ajax 网站在像这样的古老浏览器上运行,您可能可以使用旧的“隐藏 iframe”
技术hack 来完成一些事情,但是要获得几乎为零的增益需要做很多工作,并且为了支持浏览器,您仍然需要解决大量其他问题。It doesn't work in Netscape Navigator because this (ancient) browser doesn't support either the XMLHttpRequest object, nor the ActiveX alternative that works in older versions of IE.
The XMLHttpRequest object wasn't even invented when the last version of Navigator was released, and the ActiveX alternative only ever worked with IE.
If you're really desperate to make a modern Ajax site work on an ancient browser like this, you might be able to do something using the old 'hidden iframe'
techniquehack, but it'd be lot of work for virtually zero gain, and you'll still have loads of other issues to solve in order to support the browser.