AJAX - The Complete Reference

(avery) #1

80 Part I: Core Ideas


var encodedVal;
if (!encodeURIComponent)
{
encodedVal = escape(val);
/* fix the omissions */
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 createXHR()
{
try { return new XMLHttpRequest(); } catch(e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
return null;
}
function sendRequest(url, payload)
{
var xhr = createXHR();

if (xhr)
{
xhr.open("GET",url + "?" + payload,true);
xhr.onreadystatechange = function(){handleResponse(xhr);};
xhr.send(null);
}
}

function handleResponse(xhr)
{
if (xhr.readyState == 4 && xhr.status == 200)
{
var responseOutput = document.getElementById("responseOutput");
responseOutput.innerHTML = xhr.responseText;
}
}
function rate(rating)
Free download pdf