2013-06-30 50 views
2

我想通過ajax將php文件加載到div中。它在所有瀏覽器中都能正常工作,但IE6(它不會加載php文件)。我有一個任務,它需要在IE6中工作。請建議更正。我的代碼中哪部分不支持IE 6

的index.php文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Test</title> 
<script type="text/javascript"> 
window.onload = function(){ 
document.getElementById("aside").innerHTML="<img src='loadingImage.gif'>"; 
if(XMLHttpRequest) var x = new XMLHttpRequest(); 
else var x = new ActiveXObject("Microsoft.XMLHTTP"); 
x.open("GET", "other_content_1.php", true); 
x.send(""); 
x.onreadystatechange = function(){ 
    if(x.readyState == 4){ 
     if(x.status==200) document.getElementById("aside").innerHTML = x.responseText; 
     else document.getElementById("aside").innerHTML = "Error loading document"; 
     } 
    } 
} 
</script> 
</head> 

<body> 
<div id="aside">This is other content</div> 
</body> 
</html> 

other_content_1.php文件:

<div id='other-content-1'> 
<?php echo 'This text is loading via php command'; ?> 
</div> 
+0

您的代碼以何種方式不起作用? –

+0

@Ted它不加載php文件。 –

+0

我認爲需要支持IE6,你可以忘記幾乎所有令人喜愛的HTML5發明,並立即使你的工作量翻倍。除了中國之外,沒有人會使用它,請參閱http://www.ie6countdown.com/ - 它只支持大約一年,直到它終於死亡,但包括IE升級在內的替代方案都可用。 – Sven

回答

1

的IE6拋出在這條線的JavaScript錯誤:

if(XMLHttpRequest) 

這裏是該代碼適用於IE6(也可能在IE5.5上):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Test</title> 
<script type="text/javascript"> 
window.onload = function(){ 

    document.getElementById("aside").innerHTML="<img src='loadingImage.gif'>"; 

    var x = null; 

    if (window.XMLHttpRequest) { 
     var x = new XMLHttpRequest(); 
    } else if (window.ActiveXObject) { 
     var x = new ActiveXObject('MSXML2.XMLHTTP.3.0'); 
    } else { 
     // fallback 
    } 

    x.open("GET", "other_content_1.php", true); 
    x.send(""); 
    x.onreadystatechange = function() { 

     if(x.readyState == 4) { 
      if(x.status==200) 
       document.getElementById("aside").innerHTML = x.responseText; 
      else 
       document.getElementById("aside").innerHTML = "Error loading document"; 
     } 
    } 
} 
</script> 
</head> 

<body> 
<div id="aside">This is other content</div> 
</body> 
</html> 
+0

是的!有用。即使在IE5.5上也是如此。非常感謝主席。 –

+0

這意味着onreadystatechange在IE6和IE5.5中工作?關於「在Windows Internet Explorer 7中引入onreadystatechange事件」來自Microsoft Docs? – heytools

+0

@VikramRao - 我很驚訝,這在IE 5.5和6根據微軟的文檔,onreadystatechange'沒有引入'支持,直到IE 7 –

1

按照Microsoft docs,爲onreadystatechange支持在IE 7被引入;它不會在IE 6工作的解決辦法是爲執行一個同步請求,並使用結果直接:

if(window.XMLHttpRequest) { 
    var x = new XMLHttpRequest(); 
    x.open("GET", "other_content_1.php", true); 
    x.send(""); 
    x.onreadystatechange = function(){ 
     if(x.readyState == 4){ 
      if(x.status==200) document.getElementById("aside").innerHTML = x.responseText; 
      else document.getElementById("aside").innerHTML = "Error loading document"; 
      } 
     } 
    } 
} else { 
    // assume IE 6 
    var x = new ActiveXObject("Microsoft.XMLHTTP"); 
    x.open("GET", "other_content_1.php", false); // <- note change to last arg 
    x.send(""); 
    if(x.readyState == 4){ 
     if(x.status==200) document.getElementById("aside").innerHTML = x.responseText; 
     else document.getElementById("aside").innerHTML = "Error loading document"; 
     } 
    } 
} 
+0

@VikramRao - 我沒有包括你的整個代碼;使用此僅用於來自'if(XMLHttpRequest)var x = new XMLHttpRequest();'部分的部分。另外,我專注於請求本身的工作。使用'if(window.XMLHttpRequest)'而不是'if(XMLHttpRequest)'來擺脫錯誤。 –