XMLHttpRequest
!Support for XMLHttpRequest
has been added to Opera 7.60. My code
is still useful for older versions of Opera (and maybe for other browsers too),
so I'm going to leave this page here.
XMLHttpRequest
SupportThis 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.
All this started when Mendrik posted
(Opera newsserver or
Google
Groups Beta) some Javascript that emulated the functionality of the
XMLHttpRequest
GET request. The code was compatible with IE,
Mozilla/Firefox and Opera.
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.
This work is licensed under a Creative Commons License.
Attribution under the above license: Leave my name and web address in the script intact.
XMLHttpRequest
Javascript module v1.2
2005-06-14
(8393)Load this demonstration's Javascript source code into the space below.
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.
Add the script to the <HEAD>
section of your web page:
<script type="text/javascript" src="xmlhttprequest.js"></script>
Then write your code following the Gecko XMLHttpRequest
API.
Example:
var req = new XMLHttpRequest(); if (req) { req.onreadystatechange = function() { if (req.readyState == 4 && (req.status == 200 || req.status == 304)) { alert(req.responseText); } }; req.open('GET', 'pageurl.html'); req.send(null); }
Example:
var req = new XMLHttpRequest(); if (req) { req.onreadystatechange = function() { if (req.readyState == 4 && (req.status == 200 || req.status == 304)) { alert(req.responseText); } }; req.open('POST', 'scripturl.cgi'); // many server-side scripts require the Content-Type to be set: req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); req.send('var1=data1&var2=data2'); }
These have puzzled me for a while. They have now been explained to me by
"Rolf PK" who has determined that default Opera security policy
settings may be preventing the connection. These need to be overridden in the
opera.policy
file. In the "grant" section, you need to
add:
// for access to regular web servers (port 80) permission java.net.SocketPermission "*:80", "connect,resolve"; // for access to secure servers (port 443) permission java.net.SocketPermission "*:443", "connect,resolve";
Thanks Rolf!
The Opera code does not support:
responseXML
data member.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.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.Version | Date | Description |
---|---|---|
n/a | 2005-09-28 |
|
1.2 | 2005-03-30 |
|
n/a | 2004-08-24 |
|
1.1 | 2004-06-26 |
|
1.0 | 2004-06-24 |
|