Inspiriert von Dean Edwards ie7-xml-extras habe ich den XMLHttpRequest und den DOMParser noch um eine Fallback-Variante erweitert, falls das Skripting sicherer ActiveX-Plugins deaktiviert ist. Natürlich können diese Fallback-Variantenicht alles abbilden. Insbesondere der XMLHttpRequest produziert in einigen Fällen ein anderes Verhalten als ein normaler XMLHttpRequest. Das muss dann auf Seiten des Servers abgefangen werden. Die grundlegende Idee ist von Kae Verens.
Zumindest kann man so im Allgemeinen seine Daten zum Server schicken.
Viel Spass damit.
if( ! window.XMLHttpRequest ) {
// Browsers like e.g. Mozilla and Firefox have this already
try {
// MSIE Version 5 and above has an ActiveX component; MSIE 6 has a better one
var lib = /MSIE 5/.test(navigator.userAgent) ? “Microsoft” : “Msxml2”;
var test = new ActiveXObject(lib + “.XMLHTTP”);
XMLHttpRequest = function() {
return new ActiveXObject(lib + “.XMLHTTP”);
}
} catch( ex ) {
// either scripting of secure ActiveX components has been turned off or this isn’t MSIE.
// This will need a Hack“
XMLHttpRequest = function() {
// Iframe Hack
var index = 1;
XMLHttpRequest = function() {
this.readyState = 0;
this.onreadystatechange = function() {};
this._private = new Object();
this._private.iframe = document.createElement(’iframe’);
index++;
this._private.getFullUrl() {
var tmp = this.url;
var userpass = ‘’;
if( this.user ) {
userpass = this._private.user;
if( this.password )
userpass += ‘:’+this.password;
tmp = tmp.split(’://’,2);
tmp = tmp[0] + ‘://’ + userpass + ‘@’ + tmp[1];
}
return tmp;
}
this._private.sendPost = function(body) {
var form = this._private.iframe.document.createElement(’form’);
form.action = this._private.getFullUrl();
form.method = this._private.method;
form.encoding = ‘text/xml’;
var input = this,_private.iframe.document.createElement(’input’);
input.value = body;
input.name = ‘XMLHttpRequestBody’
form.appendChild(input);
form.submit();
}
this._private.sendGet = function(body) {
var tmp = this._private.getFullUrl();
tmp += ‘?XMLHttpRequestBody=’+encodeURI(body);
this._private.iframe.src = tmp;
}
var getreadystatechangefunction = function(object) {
return function() {
object.readystate = object._private.iframe.readyState;
if( !object._private.aborted )
object.onreadystatechange();
}
}
var getloadedfunction = function(object) {
return function() {
if( object._private.aborted ) {
object._private.aborted = false;
} else {
object.responseText = object._private.iframe.innerText;
objetc.reponseXML = object._private.iframe.document;
}
}
}
this._private.iframe.attachEvent (”onreadystatechange“,getreadystatechangefunction(this));
this._private.iframe.attachEvent (”onload“,getloadedfunction(this));
}
XMLHttpRequest.prototype.open = function(method,url,assync,user,password) {
this._private.method = method;
this._private.url = url;
this._private.assync = assync;
this._private.user = user;
this._private.password = password;
this._private.aborted = false;
if( method.toLowerCase() == ”get“ ) this.send = this._private.sendGet;
else if( method.toLowerCase() == ”post“ ) this.send = this._private.sendPost;
else this.send = function(body) {};
}
XMLHttpRequest.prototype.send = function(body) {} // this will be changedin open()
XMLHttpRequest.prototype.abort = function() { this._private.aborted = true; this._private.iframe.src = ”about:empty“ }
XMLHttpRequest.prototype.setRequestHeader = function(header,value) { this._private.requestHeader[header] = value; }
XMLHttpRequest.prototype.getResponseHeader = function(header) { return this._private.responseHeader[header]; }
XMLHttpRequest.prototype.getAllResponseHeaders = function() { return this._private.responseHeaderTxt; }
// not supportet - could find no way to simulate these
XMLHttpRequest.prototype.responseStream = false;
XMLHttpRequest.prototype.setRequestHeader = new Function;
XMLHttpRequest.prototype.getResponseHeader = new Function;
XMLHttpRequest.prototype.getAllResponseHeaders = new Function;
}
}
}
if( !DOMParser ) {
function DOMParser() {};
DOMParser.prototype.toString = function() {
return ”[object DOMParser]“
}
try {
var test = new ActiveXObject(”Microsoft.XMLDOM“);
DOMParser.prototype.parseFromString = function(str, contentType) {
var xmlDocument = new ActiveXObject(”Microsoft.XMLDOM“);
xmlDocument.loadXML(str);
return xmlDocument;
}
} catch( ex ) {
// this hack should hardly ever be needed
DOMParser.prototype.parseFromString = function(str, contentType) {
var myparser = document.createElement(’iframe’);
myparser.document.open();
myparser.document.write(str);
myparser.document.close();
return myparser.document;
}
}
// not supported
DOMParser.prototype.parseFromStream = new Function;
DOMParser.prototype.baseURI = ”";
}
Ich habe einen Test für meinen XMLHttpRequest-Emulator gebaut. Natürlich musste ich dafür wieder einmal ein paarBugs beheben. Darum ist hier nicht nur die HTML-Seite, von der aus der Test ausgeführt wird und das PHP-Skript, sondern auc
Aufgenommen: Dez 21, 20:34