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