Кусок кода функции:
CODE:
function SEJSLib() {
this.applyAJAXRequest = function(ajaxdarr) {
var argsN = [];
var an = 0;
for(n in ajaxdarr) {
argsN[an] = n;
an++;
}
var jsRequest = null;
try {
jsRequest = new XMLHttpRequest();
} catch(trymicrosoft) {
try {
jsRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch(othermicrosoft) {
try {
jsRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch(failed) {
jsRequest = null;
}
}
}
if(jsRequest != null) {
if(ajaxdarr[argsN[0]] != "" || ajaxdarr[argsN[1]] != "" || ajaxdarr[argsN[3]] != "") {
if(ajaxdarr[argsN[1]].toUpperCase() == "GET" || ajaxdarr[argsN[1]].toUpperCase() == "POST") {
var url = ajaxdarr[argsN[0]];
var method = ajaxdarr[argsN[1]].toUpperCase();
var usersActivity = ajaxdarr[argsN[3]];
var data = null;
if(ajaxdarr[argsN[2]] != "") {
data = ajaxdarr[argsN[2]];
}
if(method == "GET") {
if(url.indexOf("?") == -1) {
url += "?nocache=" + new Date().getTime();
} else {
url += "&nocache=" + new Date().getTime();
}
}
jsRequest.open(method,url,usersActivity);
jsRequest.onreadystatechange = function() {
if(jsRequest.readyState == 4) {
if(jsRequest.status == 200) {
alert(jsRequest.responseText);
} else {
var message = jsRequest.getResponseHeader("Status");
if(message.length = null || message.length <= 0) {
alert("Ошибка " + jsRequest.status);
} else {
alert(message);
}
}
}
}
jsRequest.send(data);
}
}
} else {
alert("Ошибка создания AJAX-запроса.");
}
}
}
function SEJSLib() {
this.applyAJAXRequest = function(ajaxdarr) {
var argsN = [];
var an = 0;
for(n in ajaxdarr) {
argsN[an] = n;
an++;
}
var jsRequest = null;
try {
jsRequest = new XMLHttpRequest();
} catch(trymicrosoft) {
try {
jsRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch(othermicrosoft) {
try {
jsRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch(failed) {
jsRequest = null;
}
}
}
if(jsRequest != null) {
if(ajaxdarr[argsN[0]] != "" || ajaxdarr[argsN[1]] != "" || ajaxdarr[argsN[3]] != "") {
if(ajaxdarr[argsN[1]].toUpperCase() == "GET" || ajaxdarr[argsN[1]].toUpperCase() == "POST") {
var url = ajaxdarr[argsN[0]];
var method = ajaxdarr[argsN[1]].toUpperCase();
var usersActivity = ajaxdarr[argsN[3]];
var data = null;
if(ajaxdarr[argsN[2]] != "") {
data = ajaxdarr[argsN[2]];
}
if(method == "GET") {
if(url.indexOf("?") == -1) {
url += "?nocache=" + new Date().getTime();
} else {
url += "&nocache=" + new Date().getTime();
}
}
jsRequest.open(method,url,usersActivity);
jsRequest.onreadystatechange = function() {
if(jsRequest.readyState == 4) {
if(jsRequest.status == 200) {
alert(jsRequest.responseText);
} else {
var message = jsRequest.getResponseHeader("Status");
if(message.length = null || message.length <= 0) {
alert("Ошибка " + jsRequest.status);
} else {
alert(message);
}
}
}
}
jsRequest.send(data);
}
}
} else {
alert("Ошибка создания AJAX-запроса.");
}
}
}
Код HTML:
CODE:
<script type="text/javascript">
var lib = new SEJSLib();
function doAJAX() {
lib.applyAJAXRequest({
rUrl: "http://localhost/AJAX/php-ajax.php",
rMethod: "GET",
rData: "",
rUsersActivity: "true"
});
}
</script>
...
<a href="javascript:doAJAX()">AJAX</a>
<script type="text/javascript">
var lib = new SEJSLib();
function doAJAX() {
lib.applyAJAXRequest({
rUrl: "http://localhost/AJAX/php-ajax.php",
rMethod: "GET",
rData: "",
rUsersActivity: "true"
});
}
</script>
...
<a href="javascript:doAJAX()">AJAX</a>
Файл php-ajax.php просто выводит случайное число.