The second of Ajax learning notes

Source: Internet
Author: User

The code is optimized as follows:

window.onload=initall;//page load is called var XHR = false;//is defined first as Flase
Load function Function initall () {    if (window. XMLHttpRequest) {//Determine if the browser supports XMLHttpRequest, and if supported, create objects     xhr= new XMLHttpRequest;     }else{        if (window. ActiveXObject) {//If not supported ActiveXObject (built-in engine object for IE early browser)              try{            xhr =  new activexobject ("Microsoft.XMLHTTP");//Create Object              }catch (e) {}        }    }        if (XHR) { //Determines if the object exists     getpix ();//calls the GetPix () function     }    else{        alert (" Sorry,but i could ' t creat an xmlhttprequest ');//Otherwise, pop-up dialog prompts cannot create a citation   &nbsp object;}} 
function getpix () {     Xhr.open ("Get/post", "URL address", "true/fasle");//opening the Open function     xhr.onreadystatechange =  showpictures;//State change judgment     xhr.end (NULL);//Send parameter (get is null,post set Requesthead first)      settimeout (getpix,5*1000);//function called once per 5 s} 
function Showpictures () {var temptext = doucument.createelement ("div");//Create a div if (xhr.readystate = 4) {//Determine whether the state is 4 if (Xhr.status = $) {var allimages = xhr.responsXML.getElementsByTagName ("content");//Processing result}}els e{alert ("There is a problem with the request" +xhr.status)}}


Places to be aware of

Although Microsoft's IE (5.5,6) supports XMLHttpRequest, there is no native version of this object, in which case the browser must be checked for ActiveX support. If supported, check that you can create XMLHttpRequest objects (try, catch for exception handling) based on ActiveX.

Whenever the Xhr.readystatue zodiac value changes, it triggers the Xhr.onreadystatuechange handler from the


ReadyState Property value

0----------------not initialized

1---------------Loading

2----------------already loaded

3----------------Interactive and can be interacted without loading or finishing

4----------------Complete


Status code 200 means everything is OK, 404 request file does not exist


The second of Ajax learning notes

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.