2011-01-23 85 views
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循环等进行扫描? 在此先感谢。 =)

+0

你在控制台中的任何错误? – 2011-01-23 13:50:42

+0

只是检查,并且在Firefox中的错误控制台atleast没有错误,正如我所说,它在Internet Explorer中,而不是在其他2浏览器。 – Anders 2011-01-23 14:04:01

回答

0

尝试这样:

ajax("http://www.tdsoft.se/index.html", function(data) { 
    document.getElementById("myDiv").innerHTML = data; 
}); 

代码

function getXmlHttpObject() { 
    var xmlHttp; 
    try { 
     xmlHttp = new XMLHttpRequest(); 
    } catch (e) { 
     try { 
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); 
     } catch (e) { 
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
    } 
    return xmlHttp; 
} 

function ajax(url, onSuccess, onError) { 
    var xmlHttp = getXmlHttpObject(); 
    xmlHttp.onreadystatechange = function() { 
     if (this.readyState == 4) { 
      if (this.status != 200) { 
       if (typeof onError == 'function') { 
        onError(this.responseText); 
       } 
      } 
      else if (typeof onSuccess == 'function') { 
       onSuccess(this.responseText); 
      } 
     } 
    }; 
    xmlHttp.open("GET", url, true); 
    xmlHttp.send(null); 
    return xmlHttp; 
}​ 
相关问题