Since XMLHttpRequest became a quasi standard, it’s annoying that the aged Internet Explorer 6 does not provide the common way of instantiation. At Microsoft – where this technics was introduced first – it’s a MSXML component that has to be loaded via ActiveX. So multiple distinctions of cases seem necessary to get quite the same object in one of the possible ways.
Now if you use this object more than once, it might be a good idea to source the instantiation out into a function. But this function could be called somehow, thus code loses portability.
That’s why I want my scripts to use nothing but var foo = new XMLHttpRequest;. So the idea is to give the Internet Explorer this object. That may sound adventuresome, but it’s surprisingly simple thanks to JavaScript’s OOP sweetness. All that has to be done is create an constructor called “XMLHttpRequest” that returns the ActiveX object.
/*@cc_on @if (@_win32 && @_jscript_version >= 5) if (!window.XMLHttpRequest)
window.XMLHttpRequest = function() { return new ActiveXObject('Microsoft.XMLHTTP') }
@end @*/
This code is commented out but uses Microsoft’s “Conditional Compilation” for being visible to MSIE. if (!window.XMLHttpRequest) checks if XMLHttpRequest really doesn’t exist yet. That won’t be the case for IE versions up to 6 but for IE7, which will provide a native XMLHTTPRequest object. I’m not using the ordinary try/catch with new ActiveXObject('Msxml2.XMLHTTP') because new ActiveXObject('Microsoft.XMLHTTP') does the job for all IE (Win) versions from 5 and should use Msxml2 automatically, if available.
Buffalo Bandit wrote on December 19, 2006 12:44 AM GMT ():
That Rocks my Socks!
You saved my buttons!
wahsagawrote on February 13, 2007 07:00 PM GMT ():
Der Code funktioniert in meinem IE 7 (Originalinstallation von MS-Webseite, also kein “gepatchter”, Version 7.0.5730.11) nicht wie beschrieben - er fragt mich beim Erstellen eines neuen XMLHttpRequest-Objektes, ob ich ActiveX zulassen möchte.
Durch Erweiterung des Codes um eine Alert-Meldung,
/*@cc_on @if (@_win32 && @_jscript_version >= 5) if (!window.XMLHttpRequest)
function XMLHttpRequest() {
alert(“Ich nutze die ActiveX-Version von XMLHttpRequest!”);
return new ActiveXObject(’Microsoft.XMLHTTP’);
}
@end @*/
zeigt er mir dann auch diese Kontrollausgabe an.
Erst ein Umschreiben der Funktions- bzw. Objekt-Deklaration in
window.XMLHttpRequest = function() { alert(“Ich nutze die ActiveX-Version von XMLHttpRequest!”); return new ActiveXObject(’Microsoft.XMLHTTP’) }
ergibt bei mir das gewünschte Ergebnis - im IE 7 _keine_ Ausgabe des Kontroll-Alerts mehr.
Christoph, ich denke, die Meldung kommt beim zweiten Mal nicht, weil du ActiveX schon beim ersten Mal zugelassen hast. ;-)
Ich sehe nicht, wie und warum deine Änderung das ansonsten umgehen sollte.
Für alle: wahsaga hat sich noch ein mal bei mir gemeldet und mir mit zwei Testseiten das Problem demonstriert. Hier ist meine Antwort:
Vergleich das mal zwischen Firefox und IE:
javascript:if(0) function x(){}; alert(typeof x)
Das scheint irgendeine JScript-Eigenart zu sein. Offensichtlich wird die Funktion auf jeden Fall deklariert, unabhängig von der Bedingung.
Das heißt, dass |if (!window.XMLHttpRequest)| für |function XMLHttpRequest() {...}| keine Rolle spielt und ggf. auch das native XMLHttpRequest-Objekt des IE7 überschrieben wird.
Ich hab den Code nun korrigiert. Danke noch mal an wahsaga.
hi.,
i’m developing in drupal theme., bt nw i had some problem that was IE6/7 browsers aren’t support my web design. then anyone pls tell how to initialize the code in my document.