
25.08.2008, 20:25
|
|
Members of Antichat - Level 5
Регистрация: 05.04.2006
Сообщений: 1,066
Провел на форуме: 3493315
Репутация:
1228
|
|
небольшой набор функций для работы с ajax
PHP код:
// (c) nc.STRIEM
var resultId;
function getXmlHttp()
{
var xmlhttp;
try
{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (E)
{
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
xmlhttp = new XMLHttpRequest();
return xmlhttp;
}
function sendRequest(url, _resultId, method, query)
{
resultId = _resultId;
var httpRequest = getXmlHttp();
var timeout;
if(method == null)
method = 'GET';
window.document.getElementById(resultId).innerHTML = '<center><img src="img/load.gif"></center>';
httpRequest.open(method, url, true);
httpRequest.onreadystatechange = function()
{
if (httpRequest.readyState != 4)
return;
clearTimeout(timeout);
if (httpRequest.status == 200)
window.document.getElementById(resultId).innerHTML = httpRequest.responseText;
else
handleError(httpRequest.statusText);
}
if(query != null)
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
httpRequest.send(query);
timeout = setTimeout( function(){ httpRequest.abort(); handleError("Time over");}, 10000); // таймаут
return false;
}
function GetContents(url, method, query)
{
var httpRequest = getXmlHttp();
var timeout;
if(method == null)
method = 'GET';
httpRequest.open(method, url, false);
if(query != null)
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
httpRequest.send(query);
if (httpRequest.status == 200)
return httpRequest.responseText;
return httpRequest.status+' '+httpRequest.statusText;
}
function handleError(message)
{
window.document.getElementById(resultId).innerHTML = message;
}
function element(id)
{
return encodeURIComponent(window.document.getElementById(id).value);
}
пример использования http://taran.su/guest/
|
|
|