The Ajax of JavaScript

Source: Internet
Author: User

Introduction: Ajax was invented in 2005 by Adaptive Path's Jesse James Garrett, the user summarizes the technology of loading page content asynchronously---say popular point, Ajax is to implement the local page through JavaScript and background server interaction, and refresh the functionality of the page.

Limitation: Ajax relies on JavaScript, so there may be browsers that are not supported (which is not the case at this time), and the search engine program may not crawl the content.

Let's start with the following points:

1, the core object of Ajax-xmlhttprequest object

This object acts as an intermediary between the JS script in the browser (which is equivalent to the client) and the server. Previous requests were made by the browser, and JavaScript was able to make requests on its own, while also handling the server's response itself. The relevant standards are also relatively new (see HTML5), but this object has a long history, so almost all browsers support, but the problem is that different browsers implement XMLHttpRequest objects in the same way. To ensure cross-browser, you have to write different branches of code for the same thing.

This is obvious in IE, different IE browser create xmlhttprequest object code different Microsoft first in IE5 with ActiveX named XMLHTTP object, So the code for creating a new XMLHTTP object in IE should be written like this:

var request=new activexobject ("msxml2.xmlhttp.3.0");

Other browsers create objects based on the XMLHttpRequest:

var request=new xmlhttprequest ();

What's more troubling is that the XMLHTTP objects used in different versions of IE are not the same. To be compatible with all browsers, the functions for creating XMLHttpRequest objects are as follows:

functionGetxmlhttprequestobject () {if(typeofXMLHttpRequest = = "undefined") {XMLHttpRequest=function () {            Try {                return NewActiveXObject ("msxml2.xmlhttp.6.0"); }            Catch(e) {return NewActiveXObject ("msxml2.xmlhttp.3.0"); }            Try {                return NewActiveXObject ("Msxml2.xmlhttp"); }            Catch(e) {}return false; }    } Else {    return NewXMLHttpRequest (); }}

After getting to play the XMLHttpRequest object, here's the next XMLHttpRequest method, he has a lot of methods, the most useful of which is the open () method.

This method has three parameters:

(1), the first parameter, used to specify that you want to access the file on the server

(2), the second parameter, which specifies the type of request to be accessed: GET, POST, SEND.

(3), whether the requested method is an asynchronous way to request

The introduction is over, words do not say, on the code:

JS Code:

//JS Ajax Startingfunctiongetnewcontent () {varRequest =Getxmlhttprequestobject (); if(Request) {Request.open ("GET", "Upload/leaning Method.txt",true); Request.onreadystatechange=function () {            if(Request.readystate = = 4) {                varPara = document.createelement ("P"); varTXT =document.createTextNode (Request.responsetext);                Para.appendchild (TXT); document.getElementsByTagName ("Body") [0].appendchild (para);        }        }; Request.send (NULL); } Else{alert ("Sorry,your Browser doesn ' t support xmlhttprequest!"); }}//JS Ajax Ending

HTML code:

<HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head>    <title></title>    <Scriptsrc= "Js/utility.js"type= "Text/javascript"></Script>    <Scriptsrc= "Js/index.js"type= "Text/javascript"></Script></Head><Body></Body></HTML>

The Ajax of JavaScript

Related Article

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.