weblog, content management & redaktion
RSS Feed für `Ajax` Seite drucken

ajaxObject(url, callbackFunction)

///////////////////////////////////////////////////////////////////////////////////////////
// Ajax Object - cns version 2011
function ajaxObject(url, callbackFunction) {
  var me=this;
  this.updating = false;
  this.abort = function() {
    if (me.updating) {
      me.updating=false;
      me.AJAX.abort();
      me.AJAX=null;
    }
  };
  this.update =
  function(passData,postMethod) {
    if (me.updating) { return false; }
    me.AJAX = null;
    if (window.XMLHttpRequest) {
      me.AJAX=new XMLHttpRequest();
    }else{
      me.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (me.AJAX==null) {
      return false;
    }else{
      me.AJAX.onreadystatechange = function() {
        if (me.AJAX.readyState==4) {
          me.updating=false;
          me.callback( me.AJAX.responseText, me.AJAX.status, me.AJAX.responseXML, me.AJAX.getAllResponseHeaders() );
          me.AJAX=null;
        }
      };
      me.updating = new Date();
      if (/post/i.test(postMethod)) {
        var uri=urlCall+(/\?/i.test(urlCall)?'&':'?')+'timestamp='+me.updating.getTime();
        me.AJAX.open("POST", uri, true);
        me.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        me.AJAX.setRequestHeader("Content-Length", passData.length);
        me.AJAX.send(passData);
      }else{
        var uri=urlCall+(/\?/i.test(urlCall)?'&':'?')+passData+'&timestamp='+(me.updating.getTime());
        me.AJAX.open("GET", uri, true);
        me.AJAX.send(null);
      }
      return true;
    }
  };
  var urlCall = url;
  this.callback = callbackFunction || function (){};
}

sendAjaxForm( form )

///////////////////////////////////////////////////////////////////////////////////////////
// Formular per Ajax senden
function sendAjaxForm( form ){       
  data = getAjaxFormData(form);   
  AXSendForm = new ajaxObject("?action=save", callBackFunc );   
  AXSendForm.update(data,"post");   
  return false;   
}

 

getAjaxFormData( form, data )

///////////////////////////////////////////////////////////////////////////////////////////
// Formular auslesen und zu AjaxPostData zusammenfügen
function getAjaxFormData( form, data ){
  data = data || "";
  for(i=0; i<form.elements.length; i++){
    el = form.elements[i];
    if(el.type!=null) switch(el.type.toLowerCase()){
      case "hidden":  
      case "text":  
      case "password":  
      case "textarea":
      case "button":
        data += "&" + el.name + "=" + encodeURIComponent(el.value);
      break;
      case "radio":
      case "checkbox":
        data += (el.checked ? "&" + el.name + "=" + encodeURIComponent(el.value) : "");
      break;
      case "select":
      case "select-one": if(el.options.length>0) data += "&" + el.name + "=" + encodeURIComponent(el.options[el.selectedIndex].value); break;
      // TODO multiselect auswerten
    }
  }
  return data;
}