0
我試圖做 的tizag例子的DOM被更改的onreadystatechange函數中,像這樣:AJAX功能目標
if(ajaxRequest.readyState == 4){
document.myForm.time.value = ajaxRequest.responseText;
}
但是我想改變多個DOM目標的值,所以我的想法是嘗試返回一個值。我修改了代碼,但顯然這不起作用。
下面是我改編的完整代碼。您可以在底部看到我嘗試訪問2個不同腳本的位置。這是在所有可能的?:
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
return ajaxRequest.responseText;
}
}
ajaxRequest.open("GET", "/time.php", true);
var time = ajaxRequest.send(null); // trying to get value from 1st script
ajaxRequest.open("GET", "/date.php", true);
var date = ajaxRequest.send(null); // trying to get values from 2nd script
document.getElementById('time').innerHTML = time; // send 1st variable to DOM
document.getElementById('date').innerHTML = date; // send 2nd variable to DOM
}