我想試驗一些代碼。我送XMLHttpRequest來一個JavaScript文件,其中我剛纔document.write('hello world');
我用在我的HTML文檔的頭部分以下代碼:對.js文件的XMLHttpRequest
<script>
function loadXMLDoc() {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "java.js", true);
xmlhttp.send();
}
</script>
和java.js只有一行代碼
document.write('hello world');
我期望的結果應該是'hello world',但它會打印整行document.write('hello world');.那麼有沒有什麼辦法只能得到'hello world'作爲responseText?使用這個文件。我知道這可以使用PHP/ASp或僅使用文本文件來完成,但這是來自我大學前兩年的挑戰,當時我是一名學生,使用javascript文件來做這件事,但我仍然沒有親愛的如何獲得這個。我想從堆棧溢出世界的優秀大腦知道這一點。
只需執行代碼。 – Anonymous
使用腳本標籤在原地注入'Hello World',簡單的Ajax不會爲此工作... – dandavis