RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Make the XMLHttpRequest Object Work Cross-Browser

Microsoft invented the XMLHttpRequest object for Internet Explorer, but it's now supported in Safari, Opera and Mozilla browsers, although the underlying implementation is different. Still, the interfaces are similar enough that—with a little effort—you can use the XMLHttpRequest object to exchange data with servers, no matter which browser is running.

he World Wide Web has traditionally used a book or slideshow metaphor as the basis for navigation between hyperlinks. Clicking on a link is like flicking from one page to another in a book or magazine, or like operating a slideshow projector—click a button or link, and a new page or slide appears. But those metaphors don't work well for all applications. This article describes a modern way to proceed when you want to expose information without changing pages. The XMLHttpRequest object—now widely available—is the answer.

The Web wasn't originally intended for application delivery, but that's what many HTML-based Web sites provide. Such applications attempt to present a user interface that is less like a book and more like a control console. Examples of real-world consoles are sound engineer's decks and air traffic control systems. Users expect to be able to operate the console controls without each operation replacing the entire console with a fresh copy. In other words, they want the console to stay where it is while they work.

Early Web applications achieved this effect with the "hidden frame" technique, using a <frame> tag with its height (or width) attribute set to 0%. Using this method, the visible frame (the console) appeared to change dynamically, while the other, hidden frame was free to shuffle data between the browser and the server as often as necessary using form submissions. The visible frame obtained fresh data from the hidden frame whenever it was updated. That approach was definitely a hack, though. It required hand crafting a simple protocol on top of HTTP, HTML, and JavaScript. Another hack used a form submission that relied on an HTTP 204 No Content response from the server, using piggybacked cookies to carry the precious return data.

A better solution is to throw out the page metaphor entirely and craft a way to submit form data directly from the current page, but without changing the current page view. That's what the XMLHttpRequest object does for you, and in such a natural way that its future popularity is assured.

What You Need
Internet Explorer, or nearly any other modern Web browser, such as Firefox, Camino, Epiphany, Mozilla Application Suite, Netscape Navigator 6+, K-Meleon, etc.

Editor's Note: The editorial staff of DevX would like to add our condolences to the voices of so many around the Web. Our longtime author and friend, Nigel McFarlane, passed away in June 2005. This article is one of two that Nigel wrote for us before his death; they are published with the permission of his family. Nigel McFarlane was the author of two books and a frequent contributor to Mozilla and the open source movement. —Lori Piquet

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date