reusable Ajax Request

Most of the time this is what we want wen making an Ajax call:

  • Send data
  • Receive result
  • Present result in an already present element

In the following function {doc} is the url to the document, {params} are the params i.e. '?thing1=this&theotherthing=that', {div} is the ID of the element to present the result.
{div} and {params} can be left blank when not needed, the {params} part can also be parsed directly in {doc}.

************ (c) 2012 DG Informatisering    *************

function ajaxReq(doc, params, div){
    var XMLHttpRequestObject = false;
      XMLHttpRequestObject = new XMLHttpRequest();
      }else if (window.ActiveXObject){
        XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");

        XMLHttpRequestObject.open("GET", doc+params, true);
        XMLHttpRequestObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        XMLHttpRequestObject.onreadystatechange = function(){
            if (XMLHttpRequestObject.readyState == 2){
            if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200){
            if(document.getElementById(div)) document.getElementById(div).innerHTML = XMLHttpRequestObject.responseText;