battles/js/ajaxLoad.js
2018-01-28 18:40:49 +02:00

80 lines
2.2 KiB
JavaScript

var xmlHttpp = [];
function ajaxLoad(url, iid, params) {
xmlHttpp[iid] = GetXmlHttpObject1();
if(xmlHttpp[iid] == null){
alert("Browser does not support HTTP Request")
return
}
xmlHttpp[iid].open("POST", url, true);
xmlHttpp[iid].onreadystatechange = function() {
var container = null;
if(xmlHttpp[iid].readyState == 4 || xmlHttpp[iid].readyState == "complete") {
if(xmlHttpp[iid].responseText.indexOf('<!--CONTAINER=') == 0) {
var a = xmlHttpp[iid].responseText.indexOf('-->');
if(a >= 0) {
var b = xmlHttpp[iid].responseText.substr(14, a-14);
if(document.getElementById(b) != undefined) {
container = document.getElementById(b);
} else{
throw "Указаный в редиректе контейнер не найден";
}
}
} else {
container = document.getElementById(iid);
}
$('#'+container.id).html(xmlHttpp[iid].responseText);
scripts = container.getElementsByTagName('script');
var loadJS = null;
for(var i = 0; i < scripts.length; i++) {
if(scripts[i].id == '') {
eval(scripts[i].text);
} else {
scriptId = scripts[i].id;
scripts[i].id = '';
if(!document.getElementById(scriptId)) {
loadJS = document.createElement("script");
loadJS.setAttribute("type","text/javascript");
loadJS.setAttribute("id",scriptId);
loadJS.text = scripts[i].text;
document.getElementsByTagName('head')[0].appendChild(loadJS);
}
scripts[i].parentNode.removeChild(scripts[i]);
i--;
}
}
$(window).trigger('ajaxLoadComplete');
}
}
xmlHttpp[iid].setRequestHeader("Accept-Charset", "windows-1251");
xmlHttpp[iid].setRequestHeader("Accept-Language", "ru, en");
xmlHttpp[iid].setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttpp[iid].setRequestHeader("Connection", "close");
var tmp = '';
for(var i in params) {
tmp += '&'+i+'='+encodeURIComponent(params[i]);
}
tmp = 'ajax_mode=load&ajax_target='+iid+tmp;
xmlHttpp[iid].send(tmp);
}
function GetXmlHttpObject1() {
var xmlHttp1 = null;
try {
xmlHttp1 = new XMLHttpRequest();
} catch(e) {
try {
xmlHttp1 = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
xmlHttp1 = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp1;
}