Do I have to be so complicated every time I create an object? The following code:
JScript Code:
"Testajax.htm" File:
Copy Code code 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"/>
Times: <input type= "text" name= "Time"/>
</form></body>
First declare a xmlHttp variable that holds the XMLHttpRequest object.
Then use Xmlhttp=new XMLHttpRequest () to create this object. This statement is for Firefox, Opera, and Safari browsers. If it fails, try the xmlhttp=new ActiveXObject ("msxml2.xmlhttp") for Internet Explorer 6.0+, and if it is unsuccessful, try XML for Internet Explorer 5.5+ Http=new ActiveXObject ("Microsoft.XMLHTTP").
If none of the three methods work, the browser used by the user is too outdated and he or she will see a hint that the browser does not support AJAX.
Can not be so troublesome, you can directly save the definition of this function as a JS file, in the need to use Ajax pages to refer to this file can be.
As an example of the following detailed:
JScript Code:
Copy Code code 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 invoked as follows:
JScript Code:
Copy Code code as follows:
var xmlHttp = Createhttpobject ();
if (!xmlhttp)
{
Return Unable to create XMLHTTP object
}
Xmlhttp.open ("Get", url, True);
Xmlhttp.onreadystatechange = function () {handlerequest (xmlHttp, Element ID)};
Xmlhttp.send (NULL);
You can also directly use jquery, a word to fix, the following code:
Copy Code code 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" > Please enter your password!) </font> ");
B=false;
}else if (!/^[a-za-z0-9_]{6,16}$/.test (password)) {
$ ("#pass"). HTML ("<font color= ' red ' > Input format is incorrect! Password should be at least 6 digits or characters </font>");
B=false;
}else{
$.get ("Loginajaxpassword", {"Userpass": encodeURI (encodeURI (password)), "username": encodeURI (encodeURI (name))}, Function (response) {
$ ("#pass"). 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>