我想通過ajax替換外部文件的div的內容。我使用這樣做的代碼是:另外通過PHP加載頁面,如果Ajax失敗
<!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>
我的問題:
如果啓用JavaScript,然後將上面的代碼工作正常,但如果JavaScript被禁用,那麼PHP應該加載該文件。我該怎麼做?
爲什麼要使用AJAX,如果你可以用你的服務器端語言來做到這一點? – Blender
[如果用戶禁用了JavaScript,如何備份到完全不同的索引頁]?(http://stackoverflow.com/questions/3936002/how-to-fallback-to-entirely-different-index-page-if - 用戶 - 擁有 - JavaScript的禁用)。這並不理想,但第一個答案可能是你最好的選擇。 – FakeRainBrigand
另請參閱本教程:http://hungred.com/how-to/determine-whether-javascript-is-enabled-disabled-via-php/ – 2013-08-02 00:32:25