How to use Ajax to create a XMLHttpRequest object _ basics

Source: Internet
Author: User
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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.