About meDashed CMS RSS
Kuragramming of beard
 
 
 /Root/Articles/

I am using Ajax

21.10.2011 19:05:39 About ajax in the Internet you can find a lot, so I will not describe. How long ago I came across a good cross-browser version of ajax and after a little modification I got to use it. Then, in his function, I added the element id and through. InnerHTML of the element replaced by the value obtained through ajax request, it is very easy and convenient when there is no need to track an event, such can be done for example in the search or to chat, check the values ​​in the database, although this approach does not working with controls, and keep track of the event is not possible.

Started to develop his CMS before I faced the task of tracking when to execute the query and return it to the control and I had to think, fortunately not for long. And I discovered an interesting feature of the language JavaScript, as a parameter to a function can be passed to another function, and as it turned out it can be done within the first, I was shocked by the sophistication of JavaScript, the shock will not last long, as a function of the parameter passed to the boom and start up where the event worked ajax and more came back row of the resulting ajax. Designed a functional as an optional parameter, so that its use is not mandatory.

Calling Ajax example: 

makeRequests('http://','post','a=gsdg&b=sdgsf','id_div_result');
makeRequests('http://','get','a=gsdg&b=sdgsf','id_div_result');
makeRequests('index.php','post','ajax=modulesopen&filename=' + '1111' ,false, function(s){alert(s)}); 
makeRequests('index.php','post','ajax=modulesopen&filename=' + '1111' ,'id') 
templates:
makeRequests('index.php','post','ajax=' ,false, function(s){}); 
makeRequests('index.php','post','ajax=','');

ajax
//
//js
function makeRequests(url,method,poststr,element, func) {
var http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');

}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
//alert(' XMLHTTP not created! ');
return false;
}
http_request.onreadystatechange = function() { alertContents(http_request,element, func); };
if (method=='get'){
http_request.open('GET', url, true);
http_request.send(null);
}

if (method=='post'){
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
http_request.setRequestHeader("Content-length", poststr.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(poststr); 
}



}
function alertContents(http_request,element, func) {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
if (element){ document.getElementById(element).innerHTML = http_request.responseText;}
if (func){func(http_request.responseText);}

} else {
if (func){func(false);}
if (element){document.getElementById(element).innerHTML = 'Server Disconnect!';}
}
}
}
//js
Download file

Рейтинг $r-- [1] $r++

Показать коментарии (vkontakte.ru)
}