A js file for creating XMLHttpRequest objects (applicable to ASP, PHP, JSP, Asp.net, etc)

Source: Internet
Author: User

Create a JS file of the XMLHTTPRequest object (applicable to ASP, PHP, JSP, Asp.net, etc)

 

 

Ajax. js

Function Ajax ()
{
VaR XMLHTTP = NULL;
If (window. XMLHttpRequest)
{// Non-ie kernel Browser
XMLHTTP = new XMLHttpRequest ();
}
Else if (window. activexobject)
{// Ie kernel Browser
Try
{// Ie6.0
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
}
Catch (E1)
{
Try
{
XMLHTTP = new activexobject ("msxml2.xmlhttp ");
}
Catch (E2)
{
Try
{
XMLHTTP = new activexobject ("msxml3.xmlhttp ");
}
Catch (E3)
{
Alert ("failed to Create Ajax:" + E3)
}
}
}
}
Else
{// Unknown Browser
Alert ("Unrecognized browsers ");
}
Return XMLHTTP;
}

 

Go to the page where you want to create an object.

<Script language = "JavaScript" type = "text/JavaScript" src = "Ajax. js"> </SCRIPT>

 

Example of ASP

<Script language = "JavaScript" type = "text/JavaScript" src = "Ajax. js"> </SCRIPT>
<Script language = "JavaScript" type = "text/JavaScript">
VaR XMLHTTP = new Ajax ();
Function checkname ()
{
XMLHTTP. onreadystatechange = getname;
XMLHTTP. Open ("get", "getname. asp", true );
XMLHTTP. Send (null)
}

Function getname ()
{
VaR dom = Document. getelementbyid ("name ")
If (XMLHTTP. readystate = 1)
{

Dom. value = "connecting to the server"
}
Else if (XMLHTTP. readystate = 2 | XMLHTTP. readystate = 3)
{

Dom. value = "reading data ";
}
Else if (XMLHTTP. readystate = 4)
{
If (XMLHTTP. Status = 200)
{

Dom. value = XMLHTTP. responsetext
}
}

}

</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.