Cross-Browser XMLHttpRequest

前段时间正为XMLHTTP的浏览器兼容性而烦恼,今天发现这片文章Cross-Browser XMLHttpRequest 比较不错。另外最新版的Opera提供了对XMLHTTP的支持,不过有些功能支持的并不是很好。附源码及示例。

Cross-Browser XMLHttpRequest v1.1
=================================

Emulate Gecko ‘XMLHttpRequest()’ functionality in IE and Opera. Opera requires
the Sun Java Runtime Environment .

by Andrew Gregory

http://www.scss.com.au/family/andrew/webdesign/xmlhttprequest/

This work is licensed under the Creative Commons Attribution License. To view a
copy of this license, visit http://creativecommons.org/licenses/by/1.0/ or send
a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305,
USA.

Introduction

Opera’s Lack of XMLHttpRequest Support
This started off when Google announced their Gmail product. The two most used browsers, IE and Mozilla/Firefox, were supported, but not the third most used browser, Opera. The main reason seemed to be Opera’s lack of support for the Mozilla/Gecko XMLHttpRequest object, itself modelled on Microsoft’s XMLHTTP ActiveX object.

Naturally, there were immediate calls for Opera Software to implement XMLHttpRequest. As with all software development, this takes time to implement and test, so Opera is not expected to support it immediately.

With some help, this module has allowed Opera to work with Gmail. See my Opera Gmail page.

Equivalent Functionality
All this started when Mendrik posted some Javascript to the Opera Page Authoring newsgroup that emulated the functionality of the XMLHttpRequest GET request. The code was compatible with IE, Mozilla/Firefox and Opera.

Faking It
That code worked, but it wasn’t call or usage compatible with existing code that used XMLHttpRequest. It occurred to me that it might be possible to develop some Javascript that would provide an emulation of a real XMLHttpRequest object. The code I developed provides a workable, but not perfect, emulation.

I expect Opera Software to soon develop native support for XMLHttpRequest, so my code is simply a stop gap measure for now.

While I originally intended to give only Opera support for XMLHttpRequest, I noticed that it was trivial to add support for Internet Explorer too. Having added that, I noticed it was just as trivial to add basic ActiveXObject emulation for “XMLHTTP” too. So now your web pages can use either the Microsoft or the Gecko-style API and with care they’ll work without modification for Internet Explorer, Mozilla/Gecko, and Opera.

When/if Internet Explorer and/or Opera eventually natively support XMLHttpRequest my code will automatically use that instead, but you might want to keep my code around for a bit to support people who haven’t updated.

Download

Creative Commons LicenseThis work is licensed under a Creative Commons License.

xmlhttprequest.js – Cross-browser XMLHttpRequest Javascript module v1.1 2004-06-26 ( 7k)

Description

Requirements
The Opera browser support requires a Java Runtime Environment. Although the code developed by Mendrik shows that it’s possible to emulate GET requests without Java, POST requests have proved to be impossible to support without Java. As POST support is half the functionality, I decided to use Java code for both GET and POST.

Usage
Add the script to the section of your web page:

<script type="text/javascript" src="xmlhttprequest.js"></script> 

Then write your code following the Gecko XMLHttpRequest API.

GET Requests
Example:

var req = new XMLHttpRequest();
if (
req) {
  
req.onreadystatechange = function() {
    if (
req.readyState == && req.status == 200) {
      
alert(req.responseText);
    }
  };
  
req.open('GET''pageurl.html');
  
req.send(null);

POST Requests
Example:

var req = new XMLHttpRequest();
if (
req) {
  
req.onreadystatechange = function() {
    if (
req.readyState == && req.status == 200) {
      
alert(req.responseText);
    }
  };
  
req.open('POST''scripturl.cgi');
  
req.send('var1=data1&var2=data2');

Unsupported Functionality

The Opera code does not support:

User/password authentication.
The responseXML data member.
Asynchronous requests are not properly supported. This appears to be because the Opera Javascript interpreter is single-threaded. The send() method will return immediately, but the actual async request will not occur until the current execution context ends. For example, if you follow send() with a long sequence of processing, the request will still not have been sent by the time it finishes.
abort() is not fully supported either. A consequence of the single-threaded nature of Opera’s Javascript interpreter is that once the request starts it cannot be stopped until it completes.
The getAllResponseHeaders and getAllResponseHeader methods are not fully supported. For me, at least, no response header information was available via the expected Java methods, so I’ve had to write some fallback code to get some limited header information from alternative methods.

View: Cross-Browser XMLHttpRequest

10 Comments »

  1. wowo said,

    December 15, 2004 @ 9:47 pm

    so cool!

  2. rainbowsoft said,

    January 11, 2005 @ 5:51 am

    在非IE浏览器中使用ActiveX控作可能并不好。
    不知道DOM标准有没有什么用呢?

  3. jason said,

    May 19, 2005 @ 1:29 pm

    req.open和req.send 的参数有哪些?各是什么意思?

  4. legend said,

    May 19, 2005 @ 3:25 pm

    req.open(bstrMethod, bstrUrl, varAsync, bstrUser, bstrPassword);
    bstrMethod:方法(post或者get等)
    bstrUrl: url地址
    varAsync: 是否为同步方式,false为异步方式
    bstrUser, bstrPassword : http认证的用户名和密码

    req.send(varBody); 要发送的数据

    可以参考msdn:
    http://msdn.microsoft.com/library/en-us/xmlsdk/html/52aaf5ff-e302-4490-821a-cb3a085fe5ee.asp?frame=true
    http://msdn.microsoft.com/library/en-us/xmlsdk/html/b0d7d002-5e8e-4bc8-be0d-b9adf50fc271.asp?frame=true

  5. guide said,

    December 21, 2006 @ 11:55 am

    Cottrell!receptacles acrimonious microprocedures snapping.mourner initialized buckskins

  6. diet pills said,

    December 23, 2006 @ 4:44 am

    terrors.fared frocks lingo adversaries clod – Tons of interesdting stuff!!!

  7. diet pill said,

    December 25, 2006 @ 5:13 am

    Nice! We truly liked this work .

  8. bet said,

    December 26, 2006 @ 7:14 am

    Hi, I really apreciated this website! Thanks

  9. islq said,

    December 28, 2006 @ 9:13 am

    varAsync[in, optional]
    A Boolean indicator as to whether the call is asynchronous. The default is True (the call returns immediately). If set to True, attach an onreadystatechange property callback so that you can tell when the send call has completed.
    按照以上MSDN的解释:
    varAsync: 是否为异步方式,false为同步方式(失去响应,直到请求完成返还),缺省为true,使用异步方式(立即返回)

    实际使用中,我这里是必须要使用true参数,否则缺省为false,即同步方式。不知道是哪里的问题。

  10. tonyktomyk said,

    July 31, 2007 @ 7:44 pm

    So here’s Heather Locklear on the beach playing football, flirting, and generally acting young. And while her body is still in great shape, the signs of ageing are starting to show. Most notably in her face, where it seems she skipped her last Botox appointment (or maybe her face-lift could use a little tightening).

    Sadly, she’s also showing signs of getting older in her arms and legs, where she’s starting to get a bit of that “old lady waddle.” In fact, the only place it looks as though she hasn’t aged is around her boobs, which are still remarkably perky (though the reason for that is probably an easy guess.)

    If you need more help making you decision, check out these Heather Locklear bikini pictures, nude pussy and tits, as well as the rest of this set after the jump.

RSS feed for comments on this post

Leave a Comment