timdodgson
Technical User
Hi guys really new to javascript have put together this function to help mw with ajax, it seems to work ok
Would like some feed back am i doing it all wrong , can i improve thank you in advance
If its really bad please dont be to harsh i have only lernt from google in last 2 weeks
/*
########################################################################
Ajax Handling
------------------------------------------------------------------------
url = sript to run on server
id = id of element to except response
trans = get / post, use which one suits your needs
sync = true - asynchronous, false - synchronous
func = function to call on completion optional
data = data to pass to script on server optional -- if you need data and no function just use a dummy value for func
------------------------------------------------------------------------
Example Post Synchronous:- ajax('script.asp','ajax','post',false,etotal,str);
Example Get Asynchronous:- ajax('script.asp','ajax','get',true,etotal,str);
########################################################################
*/
function ajax(url,id,trans,sync,func,data) {
xmlhttp=GetXmlHttpObject();
if (xmlhttp == null) {
alert ("Your browser does not support AJAX!");
return;
};
if (trans == 'get') {
url = url + '?q=' + data + '&sid=' + Math.random()
};
if (sync == true) {
xmlhttp.onreadystatechange=function(){ statechanged(id); };
};
xmlhttp.open(trans,url,sync);
if (trans == 'post') {
xmlhttp.setRequestHeader("Content-type","application/x- };
xmlhttp.send(data);
if (sync == false) {
if(xmlhttp.status == 200){
document.getElementById(id).innerHTML=xmlhttp.responseText;
};
};
if (typeof func == 'function') {
func();
};
};
function GetXmlHttpObject() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
};
if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
};
return null;
}
function statechanged(id) {
if (xmlhttp.readyState==4) {
document.getElementById(id).innerHTML=xmlhttp.responseText;
};
};
//-------------------------------- End ----------------------------------
Tim Dodgson
timdodgson@msn.com
Would like some feed back am i doing it all wrong , can i improve thank you in advance
If its really bad please dont be to harsh i have only lernt from google in last 2 weeks
/*
########################################################################
Ajax Handling
------------------------------------------------------------------------
url = sript to run on server
id = id of element to except response
trans = get / post, use which one suits your needs
sync = true - asynchronous, false - synchronous
func = function to call on completion optional
data = data to pass to script on server optional -- if you need data and no function just use a dummy value for func
------------------------------------------------------------------------
Example Post Synchronous:- ajax('script.asp','ajax','post',false,etotal,str);
Example Get Asynchronous:- ajax('script.asp','ajax','get',true,etotal,str);
########################################################################
*/
function ajax(url,id,trans,sync,func,data) {
xmlhttp=GetXmlHttpObject();
if (xmlhttp == null) {
alert ("Your browser does not support AJAX!");
return;
};
if (trans == 'get') {
url = url + '?q=' + data + '&sid=' + Math.random()
};
if (sync == true) {
xmlhttp.onreadystatechange=function(){ statechanged(id); };
};
xmlhttp.open(trans,url,sync);
if (trans == 'post') {
xmlhttp.setRequestHeader("Content-type","application/x- };
xmlhttp.send(data);
if (sync == false) {
if(xmlhttp.status == 200){
document.getElementById(id).innerHTML=xmlhttp.responseText;
};
};
if (typeof func == 'function') {
func();
};
};
function GetXmlHttpObject() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
};
if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
};
return null;
}
function statechanged(id) {
if (xmlhttp.readyState==4) {
document.getElementById(id).innerHTML=xmlhttp.responseText;
};
};
//-------------------------------- End ----------------------------------
Tim Dodgson
timdodgson@msn.com