Wednesday, December 10, 2008

AJAX Multiple request

AJAX (javascript senang citer) ni asalnya aku tolong modifykan script asal sorg membe dari ittutor ni, dia inginkan fungsi yg boleh memanggil page lain mengunakan AJAX yg berkebolehan memanggil lebih dari 1 request dalam satu2 masa... so hasil nya...
Javascript:
/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}
/* -------------------------- */
/* LOGIN */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
function login(area,urls) {
var http = createObject();
var nocache = 0;
// Optional: Show a waiting message in the layer with ID ajax_response
document.getElementById(area).innerHTML = '<center><img src=/ajax/ajax-loader.gif></center>';
http.onreadystatechange=function(){
loginReply(http, area)
}
// Set te random number to add to URL request
nocache = Math.random();
// Pass the login variables like URL variable
http.open('get', urls + '&nocache = '+ nocache,true);
http.send(null);
}
function loginReply(http,area) {
if(http.readyState == 4){
var response = http.responseText;
if(response == 0){
// if login fails
document.getElementById(area).innerHTML = 'Login failed! Verify user and password';
// else if login is ok show a message: "Welcome + the user name".
} else {
document.getElementById(area).innerHTML = 'Welcome'+response;
}
}
}
function dologin(){
login('all','all.php');
login('replies','replies.php');
}
pengunaan:
panggil fungsi dologin(), kene supply related info yg diperlukan.. 
function dologin(){
login('all','all.php');
login('replies','replies.php');
//...unlimited function call, so boleh la create sebanyak mana yg termampu dihandle oleh browser...
}
script diatas boleh diubah mengikut kesesuaian dan penggunaan.. fungsi loginReply() boleh diubah mengikut nilai yg diterima dan bentuk persembahan yg diinginkan...
have fun

No comments: