AJAX - The Complete Reference

(avery) #1

34 Part I: Core Ideas


encodedVal = encodedVal.replace(/@/g,"%40");
encodedVal = encodedVal.replace(/\//g,"%2F");
encodedVal = encodedVal.replace(/\+/g,"%2B");
}
else
{
encodedVal = encodeURIComponent(val);
/* fix the omissions */
encodedVal = encodedVal.replace(/~/g,"%7E");
encodedVal = encodedVal.replace(/!/g,"%21");
encodedVal = encodedVal.replace(/\(/g,"%28");
encodedVal = encodedVal.replace(/\)/g,"%29");
encodedVal = encodedVal.replace(/'/g,"%27");
}
/* clean up the spaces and return */
return encodedVal.replace(/\%20/g,"+");
}

function sendRequest(url, payload, target, timeout)
{
/* create request object */
var currentRequest = new Image();

/* timeout variable set later */
var timer;
/* set-up error callback */
currentRequest.onerror = function(){cancelRequest(target, "Server error",
currentRequest,timer);};

/* register callback upon load success */
currentRequest.onload = function(){handleResponse(target,currentRequest,timer);
};

/* start request */
currentRequest.src = url+"?"+payload;

/* function to cancel request if network timeout occurs */
networkTimeout = function(){cancelRequest(target, "Network timeout",currentRequest)
;};
/* define network timeout in milliseconds and bind to timeout function */
var timer = setTimeout("networkTimeout()",timeout*1000);
}

function cancelRequest(target, message, currentRequest,timer)
{
/* clear timer */
if (timer)
clearTimeout(timer);
/* clear callback */
currentRequest.onload = null;

/* set message indicator if any */
target.innerHTML = message;
}
Free download pdf