Sonntag, 12. Februar 2006
Mein Test für den XMLHttpRequest-Emulator hat dann doch noch eine zentrale Schwächen offenbart: er hat nicht funktioniert. Das habe ich inzwischen behoben, wie man sehen kann.
Der dazugehörige Code:
- ajaxlib.js:
/*************************************************************************** Copyright (C) 2005 by Christof Donat cdonat@gmx.de This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ***************************************************************************/ // fix missing features in variuos Bowsers - this should work with most more // or less modern Browsers. if( ! window.encodeURI ) { function encodeURI(text) { while( text.search(/ /) >= 0 ) text = text.replace(/ /,’%20’); return text; } } 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“ window.XMLHttpRequest = function() { var iframes_index = 1; return function () { this.readyState = 0; this.onreadystatechange = function() {}; this._private = new Object(); this._private.index = iframes_index; var body = (document.body)?document.body:document.getElementsByTagName(’body’); var iframe = document.createElement(’iframe’); iframe.name = ‘XMLHttpRequest_’+iframes_index; iframe.height = 0; iframe.width = 0; iframe.style.display = ‘none’; body.appendChild(iframe); iframes_index++; this._private.getFullUrl = function() { 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; } var eventManager = function(object, doc) { var readystatechange = function() { var iframe = eval(’frames.XMLHttpRequest_’+object._private.index); object.readyState = iframe.readyState; if( object.onreadystatechange ) object.onreadystatechange(); }; var load = function() { if( object._private.aborted ) { object._private.aborted = false; } else { var iframe = eval(’frames.XMLHttpRequest_’+object._private.index); object.responseText = iframe.innerText; object.responseXML = iframe.document; if( object.onload ) object.onload(object.reponseXML); } }; var evm = function() { if( object._private.aborted ) { object._private.aborted = false; return; } var again = true; var iframe = eval(’frames.XMLHttpRequest_’+object._private.index); if( iframe.readyState ) { if( object.readystate != iframe.readystate ) { object.readystate = iframe.readystate; readystatechange(); } if( object.readystate == 4 ) { load(); again = false; } } else { if( iframe.document && iframe.document != doc ) { object.readystate = 4; readystatechange(); load(); again = false; } } if( again ) setTimeout(evm,100); } return evm; } this.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; } this.send = function(body) { var iframe = eval(’frames.XMLHttpRequest_’+this._private.index); iframe.document.open(); iframe.document.write(’<html><head><title></title></head><body>’); iframe.document.write(’<form action=”’+this._private.getFullUrl()+’“ method=”’+this._private.method+’“>’); iframe.document.write(’<textarea name=”XMLHttpRequestBody“>’+body+’</textarea>’); iframe.document.write(’</form></body></html>’); iframe.document.close(); iframe.document.forms[0].submit(); setTimeout(eventManager(this, iframe.document),100); } this.abort = function() { this._private.aborted = true; var iframe = eval(’frames.XMLHttpRequest_’+this._private.index); iframe.location.href = ”about:empty"; } this.setRequestHeader = function(header,value) { this._private.requestHeader[header] = value; } this.getResponseHeader = function(header) { return this._private.responseHeader[header]; } this.getAllResponseHeaders = function() { return this._private.responseHeaderTxt; } this.onreadystatechange = function() {}; // not supportet - could find no way to simulate these this.responseStream = false; this.setRequestHeader = new Function; this.getResponseHeader = new Function; this.getAllResponseHeaders = new Function; } } (); } }
- test.php
<? header(’Content-type: text/xml’); if( $_REQUEST[’XMLHttpRequestBody’] ) { print ‘<text response=“emulation”>’; print $_REQUEST[’XMLHttpRequestBody’]; print ‘</text>’; }else { print ‘<text response=“native”>’; print file_get_contents(“php://input”); print ‘</text>’; } ?>
- test.html
<HTML> <HEAD> <TITLE> XMLHttpRequest test </TITLE> <SCRIPT LANGUAGE=“JavaScript” SRC=“ajaxlib.js” TYPE=“text/javascript”></script> <script language=“JavaScript”> var init; function doserialize(node) { switch(node.nodeType) { case 1: { var rval = ‘<’+node.nodeName; for( var i = 0; i < node.attributes.length; i++ ) { rval += ' ‘+node.attributes[i].nodeName+’=“’+node.attributes[i].nodeValue+’”’; } if( node.childNodes.length > 0 ) { rval += ‘>’; for( var i = 0; i < node.childNodes.length; i++ ) rval += doserialize(node.childNodes[i]); rval += ‘</’+node.nodeName+’>’; } else { rval += ' />’; } return rval; } case 3: return node.nodeValue; } } function serializeXML(doc) { return doserialize(doc.documentElement); } testXMLHttpRequest = function() { var test; init = function() { test = new XMLHttpRequest(); } return function() { test.open(’POST’,’test.php’); test.onload = function() { var doc = this.responseXML; alert(serializeXML(doc)); }; test.send(’ test ’) } }() </script> </HEAD> <BODY onload=“init()”> <a href=“javascript: testXMLHttpRequest()”>test</a> </BODY> </HTML>
|