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

raws on 09/30/06


Tagged

ajax javascript


Versions (?)


Who likes this?

3 people have marked this snippet as a favorite

Hollow
shachi
vali29


Ajax Request Object Constructor


Published in: JavaScript 


  1. // ----------------------------------------
  2. // Wrapper function for constructing a request object.
  3. // Parameters:
  4. // reqType: The HTTP request type, such as GET or POST.
  5. // url: The URL of the server program.
  6. // asynch: Whether to send the request asynchronously or not.
  7. // ----------------------------------------
  8.  
  9. function httpRequest(reqType,url,asynch) {
  10.  
  11. // Mozilla-based browsers
  12. if (window.XMLHttpRequest) {
  13. request = new XMLHttpRequest();
  14. } else if (window.ActiveXObject) {
  15. request = new ActiveXObject("Msxml2.XMLHTTP");
  16. if (!request) {
  17. request = new ActiveXObject("Microsoft.XMLHTTP");
  18. }
  19. }
  20.  
  21. // Request could still be null if neither ActiveXObject
  22. // initialization succeeded
  23. if (request) {
  24. initReq(reqType,url,asynch);
  25. } else {
  26. alert("Your browser does not permit the use of all " +
  27. "of this application's features!");
  28. }
  29.  
  30. }
  31.  
  32. // ----------------------------------------
  33. // Initialize a request object that is already constructed
  34. // ----------------------------------------
  35.  
  36. function initReq(reqType,url,asynch) {
  37. // Specify the function that will handle the HTTP response
  38. request.onreadystatechange = handleResponse;
  39. request.open(reqType,url,bool);
  40. request.send(null);
  41. }

Report this snippet 

You need to login to post a comment.