2011-01-23 87 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; 
}​ 
相關問題