We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

damarev on 08/13/07


Tagged

ajax XHConn


Versions (?)


Simple XMLHTTP Interface


Published in: JavaScript 


URL: http://xkr.us/code/javascript/XHConn/

  1. /** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08 **
  2.  ** Code licensed under Creative Commons Attribution-ShareAlike License **
  3.  ** http://creativecommons.org/licenses/by-sa/2.0/ **/
  4. function XHConn()
  5. {
  6. var xmlhttp, bComplete = false;
  7. try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  8. catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  9. catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  10. catch (e) { xmlhttp = false; }}}
  11. if (!xmlhttp) return null;
  12. this.connect = function(sURL, sMethod, sVars, fnDone)
  13. {
  14. if (!xmlhttp) return false;
  15. bComplete = false;
  16. sMethod = sMethod.toUpperCase();
  17.  
  18. try {
  19. if (sMethod == "GET")
  20. {
  21. xmlhttp.open(sMethod, sURL+"?"+sVars, true);
  22. sVars = "";
  23. }
  24. else
  25. {
  26. xmlhttp.open(sMethod, sURL, true);
  27. xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
  28. xmlhttp.setRequestHeader("Content-Type",
  29. "application/x-www-form-urlencoded");
  30. }
  31. xmlhttp.onreadystatechange = function(){
  32. if (xmlhttp.readyState == 4 && !bComplete)
  33. {
  34. bComplete = true;
  35. fnDone(xmlhttp);
  36. }};
  37. xmlhttp.send(sVars);
  38. }
  39. catch(z) { return false; }
  40. return true;
  41. };
  42. return this;
  43. }

Report this snippet 

You need to login to post a comment.