0
我有一个快速的问题,下面只适用于IE 7及以上版本,我怎样才能使它在Firefox和Opera上工作?你如何让这个ajax例子在opera和firefox上工作?
<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","http://www.tdsoft.se/index.html",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>
编辑
感谢您的回答,我现在只是有关于followng代码
<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)
{
return xmlhttp.responseText;
}
}
xmlhttp.open("GET","index.html",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>
现在我想通过xmlhttp.responseText搜索另一个问题(换言之,称为函数loadXMLDoc())用于关键字,例如“te stfile”,如果它存在多个例如 “testfile_1” 和 “testfile_2” ..... “testfile_n” 然后“doSomething的”
这样
function searchADocument(wordToSearchFor){
int numberOfTimesWordOccurs=0;
var thePageToSearchThrough [] = loadXMLDoc();
for (i=0; i<thePageToSearchThrough.length; i++){
if(thePageToSearchThrough[i]==wordToSearchFor)
numberOfTimesWordOccurs++;
}
If (wordToSearchFor > 1)
document.write("<a href="http://selnc05.go.se:8080/component_test/build/testfile_1"> testfile_1</a>"<a href="http://selnc05.go.se:8080/component_test/build/testfile_2"> testfile_2</a><a href="http://selnc05.go.se:8080/component_test/build/testfile_n"> testfile_n</a>
)
Else
window.location="http://selnc05.go.se:8080/component_test/build/testfile.html";
}
我不知道从哪里开始,因为我不知道xmlhttp.responseText是什么类型,我可以将它存储在数组中并使用for循环等进行扫描? 在此先感谢。 =)
你在控制台中的任何错误? – 2011-01-23 13:50:42
只是检查,并且在Firefox中的错误控制台atleast没有错误,正如我所说,它在Internet Explorer中,而不是在其他2浏览器。 – Anders 2011-01-23 14:04:01