[Skip top navbar]

Andrew Gregory's Web Pages

View of Perth city from Kings Park, 31°57'19"S 115°51'2"E

-

Cross-Browser XMLHttpRequest


Opera 7.60(beta) supports 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.


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 (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.

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 License
This work is licensed under a Creative Commons License.

Attribution under the above license: Leave my name and web address in the script intact.


Demonstration

Load this demonstration's Javascript source code into the space below.


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 <HEAD> 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 == 4 && (req.status == 200 || req.status == 304)) { 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 == 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'); }

"Connection failed" security exceptions

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!

Unsupported Functionality

The Opera code does not support:


Version History

XMLHttpRequest Javascript Version History
VersionDateDescription
n/a2005-09-28
  • Added notes in relation to the "Connection failed" security exceptions.
1.22005-03-30
  • Added getResponseHeader.
n/a2004-08-24
  • Opera 7.60 has added support for XMLHttpRequest.
1.12004-06-26
  • Added ActiveXObject emulation.
1.02004-06-24
  • Initial release.

-