Especially after the popularity of Jquery, porotype, Ext and other frameworks, a large amount of simplified code can be said to bring the gospel to programmers,
Free from the heavy work of code, you can easily implement the complex functions of writing in the past.
However, there are advantages and disadvantages. Nowadays, programmers who are used to various frameworks and seldom write their own internal source code, there are still a few who remember that Ajax is not actually a complex basic code?
Today, in order not to forget my original enthusiasm, I will post this basic code and review it for myself.
1. For Ajax, judging the browser category is a repetitive and innovative task. This code can be completely encapsulated and called at will.Copy codeThe Code is 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;
}
If you don't understand it, it's okay to copy it directly.
2. The next step is to submit the Url code.
The entire process is similar. Most codes can be encapsulated. You only need to pass in the parameters and obtain the results.
Copy codeThe Code is as follows: function Ajax (url)
{
Var XmlHttp = new xmlhttp ();
Try
{
XmlHttp. open ("POST", url, true );
Var result = SendUrl ();
}
Catch (e)
{
Alert (e );
}
}
3.Copy codeThe Code is as follows: function SendUrl ()
{
XmlHttp. send ();
XmlHttp. onreadystatechange = function ()
{
If (XmlHttp. readyState = 4 & XmlHttp. status = 200)
{
Var result = XmlHttp. responseText;
Return result;
}
}
}