AJAX example: compile a simple album with AJAX. The Javascript tutorial AJAX (Asynchronous JavaScript And XML) is a JavaScript And Extensible Markup Language (XML ), technology for transmitting or receiving data between a web browser and a server. It is mainly used in the development of Ria (Rich internet applications.
The xml problem is finally solved today. Finally, some old methods of Dom are used in Firefox. I will explain the method here.
Code:
Var XmlHttp; // defines an xmlHttpRequest object. Var temp_url_arr = new Array () Var temp_title_arr = new Array () Var list_arr = new Array () If (window. XMLHttpRequest ){ XmlHttp = new XMLHttpRequest () If (XmlHttp. overrideMimeType ){ XmlHttp. overrideMimeType ('text/xml '); } } Else if (window. ActiveXObject ){ XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP "); } // The above section judges the current browser version to define different xmlHttpRequest objects for XmlHttp. If the server response does not have the XML mime-type header, some Mozilla browsers may not work properly. Therefore, XmlHttp. overrideMimeType ('text/xml') is required to modify the header.
Function getData () {// read data XmlHttp. onreadystatechange = praseXml; XmlHttp. open ("GET", "xmldata2.xml", true ); XmlHttp. send (null ); }
Function praseXml () {// parse data If (XmlHttp. readyState = 4 ){ If (XmlHttp. status = 200 ){ Var xmldoc = XmlHttp. responseXML; Var root = xmldoc. getElementsByTagName ('data'). item (0) // Use this method to take effect in Firefox. At least I have never succeeded in xPath. For (var iRoot = 0; iRoot <root. childNodes. length; iRoot ++ ){ // Alert (root. childNodes. item (iRoot )) Var pic_node = root. childNodes. item (iRoot) For (iPic = 0; iPic <pic_node.childNodes.length; iPic ++ ){ Var url_node = pic_node.childNodes.item (iPic) For (iURL = 0; iURL <url_node.childNodes.length; iURL ++ ){ Var obj = new Object () Obj. type = url_node.nodeName Obj. content = url_node.childNodes.item (iURL). nodeValue If (url_node.nodeName = "url "){ Temp_url_arr.push (obj) } Else if (url_node.nodeName = "title "){ Temp_title_arr.push (obj) } } } } Install_list () } } }
Function install_list () {// the collected data is loaded into the list_arr array. List_arr = new Array () Var target_div = document. getElementById ('catelog '); Target_div.innerHTML = "" For (var I = 0; I Var obj = new Object () Obj. url = temp_url_arr [I]. content Obj. title = temp_title_arr [I]. content List_arr.push (obj) } For (var I = 0; I Target_div.innerHTML + = "" + list_arr [I]. title +" "; } } Function img_loader (param1) {// Method for loading Images Var target_div = document. getElementById ('pic '); Target_div.innerHTML = "" } |