In particular, after the popularity of frameworks such as Jquery,porotype,ext, a lot of simplified code can be said to bring the gospel to programmers,
From the heavy work of the code to release, easy to achieve the past to write complex functions.
But pros and cons, until now, accustomed to using a variety of frameworks, and rarely write their own internal source code of the program staff, there are a few remember Ajax that is not really complicated basic code?
Today, in order not to forget the original enthusiasm, I put the basis of the code and then posted, to their own review.
1. For Ajax, judging a browser's category is a repetitive and uninspired job, and this code can be encapsulated and invoked randomly.
Copy Code code as follows:
function xmlhttp ()
{
var a=null
Try
{
A=new ActiveXObject ("msxml2.xmlhttp");
}
catch (E)
{
Try
{
A=new ActiveXObject ("Microsoft.XMLHTTP");
}
catch (OC)
{
if (typeof xmlhttprequest!= "Undefined")
{
A=new XMLHttpRequest ();
}
}
}
return A;
}
Do not explain, anyway, even if you do not understand, directly copied past use is no problem.
2. Next is the code that submits the URL.
The whole process is very much the same, most of the code can actually be encapsulated, as long as the parameters passed in, and then get the results can be.
Copy Code code as follows:
function Ajax (URL)
{
var xmlhttp=new XmlHttp ();
Try
{
Xmlhttp.open ("POST", url,true);
var result=sendurl ();
}
catch (E)
{
Alert (e);
}
}
3.
Copy Code code as follows:
function Sendurl ()
{
Xmlhttp.send ();
Xmlhttp.onreadystatechange=function ()
{
if (xmlhttp.readystate==4 && xmlhttp.status==200)
{
var Result=xmlhttp.responsetext;
return result;
}
}
}