javascript - Does this Ajax method work in many browsers and operating systems? -
i'm using ajax in javascript values on other pages.
function loadxmldoc(){ var xmlhttp; if (window.xmlhttprequest) {// code ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest(); } else {// code ie6, ie5 xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { document.getelementbyid("mydiv").innerhtml=xmlhttp.responsetext; } } xmlhttp.open("get","ajax_info.txt",true); xmlhttp.send(); }
but i'm afraid not work in browsers or operating systems. safe use? or better use jquery ajax?
(detail: not want use jquery!)
first off up-vote trying real javascript!
secondly unaware you're treating code text, not code. don't use innerhtml
, don't use responsetext
. need use proper dom method such appendchild
, insertbefore
though ajax need use importnode
, responsexml
. xml load server must served application/xml
, mime text/xml
invalid.
the code on site works older browsers (e.g. ie5/6 if you're insane @ point). i've been meaning clean up. it's reusable won't need keep creating new ajax functions except ajax_post()
if visit site , @ index.js
file you're welcome mess that.
super-bonus: ajax_sequel()
function can used execute whatever want after ajax request has completed in full. i've left alert
s in place mess code.
example execution...
ajax_load_page('?ajax=1&url=mail/sent/','inside','sidebar','sequel-string',id_focus);
the first parameter ajax url, keep in mind must have single element contains everything request , no whitespace before or after it. must also have xml namespace...
<div xmlns="http://www.w3.org/1999/xhtml">ajax stuffs</div>
the second parameter accepts strings after
, before
, inside
, replace
relative id of third parameter.
the fourth parameter use ajax_sequel()
function mentioned.
the last (fifth) parameter id want browser give focus (if any).
strict code difficult when right way things work easier.
any functions missing can found in index.js
file of site, linked profile. here main part of you're looking for...
function ajax_id_duplication_prevention(xml) {//alert(typeof xml+'\n\n'+xml.childnodes[0].childnodes.length); var re = true; if (option.id_fade && option.id_fade!='' && document.getelementbyid(option.id_fade)) { element_del(option.id_fade); option.id_fade = ''; } if (typeof document.createtreewalker=='function') { var idz = []; try { var walker = document.createtreewalker(xml,nodefilter.show_element,null,false); while (walker.nextnode()) { if (walker.currentnode.id==undefined && walker.currentnode.nodename=='parsererror') {alert('error: parser error detected.\n\nthis may or may not afflict content being loaded.\n\nif content not load correctly reload entire page.');} else if (walker.currentnode.id==undefined) {alert('walker.currentnode.nodename = '+walker.currentnode.nodename+'\n\n'+document.serializetostring(xml));} else if (walker.currentnode.id!='') { var n = document.getelementbyid(walker.currentnode.id); if (n) { var l = document.getelementbyid('liquid'); (var i=0; i<l.childnodes.length; i++) { var c = l.childnodes[i]; if (n.comparedocumentposition(c)==10) { element_del(c); //do ajax report db table: id error log break; } } //alert('error: can not import xml.\n\nan element id \''+walker.currentnode.id+'\' exists in target application.'); //re = false; break; } else if (in_array(walker.currentnode.id,idz)) { alert('error: can not import xml, id \''+walker.currentnode.id+'\' detected twice in layer being imported.\n\nduplicated id\'s break expected functionality , illegal.\n\nwhile xml content not imported still possible related request successful.'); re = false; break; } else {idz.push(walker.currentnode.id);} } } } catch (err) {}//ie9 } return re; } function ajax_load_page(url,id_container_pos,id_container,sequel,id_focus) {//alert(url+'\n'+id_container_pos+'\n'+id_container+'\n'+sequel+'\n'+id_focus); if (document.getelementbyid(id_container) || id_container) { if (window.xmlhttprequest) {var xmlhttp = new xmlhttprequest();} else if (window.activexobject) {try {xmlhttp = new activexobject('msxml2.xmlhttp');} catch (e) {try {xmlhttp = new activexobject('microsoft.xmlhttp');} catch (e) {}}} else {alert('error: browser not seem support ajax.');} xmlhttp.open('get',url,true); xmlhttp.send(null); xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate=='4') { if (xmlhttp.status=='200' || xmlhttp.status=='401' || xmlhttp.status=='403' || xmlhttp.status=='404' || xmlhttp.status=='501') {ajax_load_page_import(url,xmlhttp,id_container_pos,id_container,sequel,id_focus);} else if (xmlhttp.status=='404') {alert('http 404: content not found.');} else if (xmlhttp.status=='429') {alert('http 429: requesting pages quickly.');} else if (xmlhttp.status=='500') { if (xmlhttp.getresponseheader('ajax-http')==null) {alert('http 500: server encountered error.');} else { var http = xmlhttp.getresponseheader('ajax-http'); if (http==429) {alert('http 429: requesting pages quickly.');} else {alert('http '+http);} } } else if (xmlhttp.status=='204' || xmlhttp.status=='0' || xmlhttp.status=='1223' || xmlhttp.status==undefined) {if (id_container=='chat_messages') {ajax_sequel(sequel,id_container);}} else if (xmlhttp.status!='204' && xmlhttp.status!='0' && xmlhttp.status!='1223') {alert('http '+xmlhttp.status+'\n\nif keep encountering error please contact webmaster.');}//opera 204='0' & ie 204='1223' } } } else {alert('error: '+id_container+' id not exist!');} } function ajax_load_page_import(url,xmlhttp,id_container_pos,id_container,sequel,id_focus) {//alert('url = '+url+'\n\nxmlhttp = '+xmlhttp+'\n'+id_container_pos+'\n'+id_container+'\n'+sequel+'\n'+id_focus); if (!document.getelementbyid('body') && xmlhttp.responsexml) {var xmldoc=xmlhttp.responsexml;} else {var xmldoc=xmlhttp.responsetext;} if (!document.element_node) { document.element_node = 1; document.attribute_node = 2; document.text_node = 3; document.cdata_section_node = 4; document.entity_reference_node = 5; document.entity_node = 6; document.processing_instruction_node = 7; document.comment_node = 8; document.document_node = 9; document.document_type_node = 10; document.document_fragment_node = 11; document.notation_node = 12; } document._importnode = function(node,allchildren) { switch (node.nodetype) { case document.element_node: var newnode = document.createelement(node.nodename); if (node.attributes && node.attributes.length > 0) {for (var = 0, il = node.attributes.length; < il;) {newnode.setattribute(node.attributes[i].nodename, node.getattribute(node.attributes[i++].nodename));}} if (allchildren && node.childnodes && node.childnodes.length > 0) {for (var = 0, il = node.childnodes.length; < il;) {newnode.appendchild(document._importnode(node.childnodes[i++], allchildren));}} return newnode; break; case document.text_node: case document.cdata_section_node: case document.comment_node: return document.createtextnode(node.nodevalue); break; } return true; } if (xmlhttp.responsexml) { if (ajax_id_duplication_prevention(xmlhttp.responsexml)) { if (xmlhttp.responsexml.childnodes.length==0) {alert('error: no elements found in ajax request!\n\n'+url);} else if (xmlhttp.responsexml.childnodes.length>1) {alert('error: parse error, ajax requests can have single parent-most element.\n\n'+url+'\n\n'+xmlhttp.responsetext);} else { if (document.getelementbyid(id_container)) {var id_container_obj = document.getelementbyid(id_container);} else {var id_container_obj = id_container;} var id_ajax = xmlhttp.responsexml.childnodes[0].getattribute('id'); if (id_container=='prompts_ajax') {ajax_layer_init('prompts_ajax',id_ajax);} if (document.importnode && xmlhttp.responsexml && document.getelementbyid('body').style.khtmlmarginbottomcollapse==undefined && browser!='msie') { if (id_container_pos=='after') {id_container_obj.insertbefore(xmldoc.getelementsbytagname('div')[0],id_container_obj.nextsibling);} else if (id_container_pos=='before') { id_container_obj.parentnode.insertbefore(document.importnode(xmldoc.getelementsbytagname('div')[0],true),id_container_obj); } else if (id_container_pos=='inside') {id_container_obj.appendchild(document.importnode(xmldoc.getelementsbytagname('div')[0],true));} else if (id_container_pos=='replace') {var r = id_container_obj.parentnode; r.removechild(r.getelementsbytagname('div')[0]); r.appendchild(document.importnode(xmldoc.getelementsbytagname('div')[0],true));} else {alert('error: unknown position import data to: '+id_container_pos);} } else if (!document.getelementbyid('body') && xmlhttp.responsexml)//ie8+ { if (typeof xmldoc.getelementsbytagname('div')[0]=='object') { if (id_container_pos=='after') {id_container_obj.parentnode.appendchild(document._importnode(xmldoc.getelementsbytagname('div')[0],true));} else if (id_container_pos=='before') {id_container_obj.parentnode.insertbefore(document._importnode(xmldoc.getelementsbytagname('div')[0],true),id_container_obj);} else if (id_container_pos=='inside') {id_container_obj.appendchild(document._importnode(xmldoc.getelementsbytagname('div')[0],true));} else if (id_container_pos=='replace') {var r = id_container_obj.parentnode; r.removechild(r.getelementsbytagname('div')[0]); r.appendchild(document._importnode(xmldoc.getelementsbytagname('div')[0],true));} else {alert('error: unknown position import data to: '+id_container_pos);} //if (document.getelementbyid(id_focus)) {document.getelementbyid(id_focus).focus();} } } else if (document.getelementbyid('body') && xmlhttp.responsexml)// ie 5.0~7 { if (document.getelementbyid('body').currentstyle.scrollbarbasecolor) { //ie 5.5/6/7 var id_imported = xmlhttp.responsexml.childnodes[0].getattribute('id'); if (!document.getelementbyid(id_imported)) { if (id_container_pos=='after') {id_container_obj.parentnode.appendchild(document._importnode(xmlhttp.responsexml.childnodes[0],true));} else if (id_container_pos=='before') {id_container_obj.parentnode.insertbefore(document._importnode(xmlhttp.responsexml.childnodes[0],true),id_container_obj);} else if (id_container_pos=='inside') {id_container_obj.appendchild(document._importnode(xmlhttp.responsexml.childnodes[0],true));} else if (id_container_pos=='replace') {var r = id_container_obj.parentnode.id; id_container_obj.parentnode.removechild(id_container_obj.parentnode.getelementsbytagname('div')[0]); if (document.getelementbyid(r)) {document.getelementbyid(r).appendchild(document._importnode(xmlhttp.responsexml.childnodes[0],true));}} else {alert('error: unknown position import data to: \''+id_container_pos+'\'');} } } var id_ajax = xmlhttp.responsexml.childnodes[0].getattribute('id'); } else if (!id_container_obj) {alert('error: can not add content dom; id \''+id_container+'\' not exist or has not been imported dom yet.');} else {alert('error: ajax function did not trigger correctly, try checking content\'s mime?\n\n'+xmlhttp+'\n'+id_container_pos+'\n'+id_container+'\n'+sequel+'\n'+id_focus);} ajax_sequel(sequel,id_container,id_ajax,id_focus); } } } else if (xmlhttp.getresponseheader('content-type')=='text/html') { if (!option.th || option.th==2) { ajax_load_page(url,id_container_pos,id_container,sequel,id_focus); option.th = 1; } else if (confirm('error: unable load ajax content.\n\ntype: '+xmlhttp.getresponseheader('content-type')+'\n\ninitiating second attempt @ request...\n\n'+xmlhttp.responsetext)) { ajax_load_page(url,id_container_pos,id_container,sequel,id_focus); } } else { alert('error: unable load ajax content.\n\ntype: '+xmlhttp.getresponseheader('content-type')+'\n\nresponsetext = '+xmlhttp.responsetext); } } function ajax_sequel(sequel,id_container,id_ajax,id_focus) {alert('ajax_sequel()\n\nsequel = '+sequel+'\nid_container = '+id_container+'\nid_ajax = '+id_ajax); }
Comments
Post a Comment