Every time I create an object, is it so complicated? The following code:
JScript code:
"TestAjax.htm" file:
Copy codeThe Code is as follows: <Body>
<Script type = "text/javascript">
Function ajaxFunction ()
{
Var xmlHttp;
Try
{
// Firefox, Opera 8.0 +, Safari
XmlHttp = new XMLHttpRequest ();
}
Catch (e)
{
// Internet Explorer
Try
{
XmlHttp = new ActiveXObject ("Msxml2.XMLHTTP ");
}
Catch (e)
{
Try
{
XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Catch (e)
{
Alert ("your browser does not support AJAX! ");
Return false;
}
}
}
}
</Script>
<Form name = "myForm">
User: <input type = "text" name = "username"/>
Time: <input type = "text" name = "time"/>
</Form> </body>
</Html>
First, declare an xmlHttp variable that saves the XMLHttpRequest object.
Then use XMLHttp = new XMLHttpRequest () to create this object. This statement is applicable to Firefox, Opera, and Safari browsers. If it fails, try xmlHttp = new ActiveXObject ("Msxml2.XMLHTTP") for Internet Explorer 6.0 +. If it fails, then try xmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ").
If these three methods do not work, the browser used by the user is too outdated. He or she will see a prompt stating that the browser does not support AJAX.
You don't have to worry about it. You can directly Save the definition of this function as a js file and reference this file on the page that requires AJAX.
For example:
JScript code:Copy codeThe Code is as follows: function CreateHTTPObject ()
{
Var xmlhttp;
Try
{
Xmlhttp = new ActiveXObject ("Msxml2.XMLHTTP ");
}
Catch (e)
{
Try
{
Xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Catch (e)
{
Xmlhttp = false;
}
}
If (! Xmlhttp & typeof XMLHttpRequest! = 'Undefined ')
{
Try
{
Xmlhttp = new XMLHttpRequest ();
}
Catch (e)
{
Xmlhttp = false;
}
}
If (! Xmlhttp & window. createRequest)
{
Try
{
Xmlhttp = window. createRequest ();
}
Catch (e)
{
Xmlhttp = false;
}
}
Return xmlhttp;
} Define the above function and create an instance when calling it, as shown below:
JScript code:Copy codeThe Code is as follows: var xmlHttp = CreateHTTPObject ();
If (! XmlHttp)
{
Return; // The xmlhttp object cannot be created.
}
XmlHttp. open ("GET", url, true );
XmlHttp. onreadystatechange = function () {HandleRequest (xmlHttp, "element ID ")};
XmlHttp. send (null );
You can also use jquery directly, with the following code:Copy codeThe Code is as follows: $ (document). ready (function (){
$ ("# Userpass"). blur (function (){
Var password = $ ("# userpass"). val ();
Var name = $ ("# username"). val ();
If (password = "" | password = null ){
$ ("# Pass" ).html ("<font color = 'red'> enter the password! </Font> ");
B = false;
} Else if (! /^ [A-zA-Z0-9 _] {6, 16} $/. test (password )){
$ ("# Pass" ).html ("<font color = 'red'> the input format is incorrect! The password should be at least 6 digits or characters </font> ");
B = false;
} Else {
$. Get ("LoginAjaxPassword", {"userpass": encodeURI (password), "username": encodeURI (name)}, function (response ){
$ ("# Pass" response .html (response );
If (response = "<font color = 'green' size = '2'>" + "√" + "</font> "){
B = true;
}
});
}
Return B;
});
$ ("# Login-submit"). click (function (){
Var autologin = document. getElementById ("autologin"). checked;
If (a & B ){
// If ($ ("# autologin"). attr ("checked") = true ){
If (autologin = true ){
// $ {"# Login-user-form"}. attr ("action", "AutoLogin ");
// $ ("# Login-user-form"). submit ();
Document. form. action = "AutoLogin ";
Document. form. submit ();
} Else {
// $ {"# Login-user-form"}. attr ("action", "Login ");
// $ ("# Login-user-form"). submit ();
Document. form. action = "Login ";
Document. form. submit ();
}
} Else {}
});
});
</Script>