Kae Verens

AJAX in IE without ActiveX

by kae verens on Aug.12, 2005, under ajax, javascript

What a day! Recently, one of our customers was saying that his site wasn’t working whenever he viewed it in his local net-cafe. This was incredible, to me, as every computer I’ve checked it on has displayed fine.

I finally had an inkling of an idea why this was happening, when I asked him to “click help->about in the browser”, to find out what exact version this was happening in. He said that the browser wasn’t allowing him to access that information.

I understood immediately that it was a security thing. Net-cafes would naturally hate to be infected with the myriad viruses out there, so they tend to belt-up their browsers tighter than the average user.

So, I tried to replicate what might be going on – I set my own Internet Explorer to stun – High security setting. When I tried the site, I was not surprised to find that it was not working. The fact that it relies on JavaScript made that a certainty anyway.

To make it work, I needed to turn on “Active Scripting” (what IE calls ECMAScript), and let it run safe ActiveX modules.

Naturally, I’m slightly ashamed that the site doesn’t work properly in a non-javascript environment, but the site is aimed at the overwhelmingly vast majority of people who are normal. The ActiveX thing, though, was a problem.

I began searching through the source of every AJAX library I could think of – Sarissa, JSPan, Sajax, ie7-xml-extras – every single one of them relied on ActiveX.

Surely there must be some XMLHttpRequest library out there that doesn’t rely on ActiveX?

I sat down, and started work on one. It’s Friday, and I’m exhausted after a very long day’s research, so I won’t go too in-depth into it…

Here’s the file

There are some problems with the above, which I’ll detail.

First off, the file is a drop-in emulation of the XMLHttpRequest object. I wanted to use the exact same API as every other library on the planet is using.

The short explanation of how it works: the library creates an <iframe>, populates it with the requested data, then returns the source of that object.

That’s not quite as simple as it sounds. I got 90% of the code working in 10% of my working time today, and spent the next 90% of the day trying, and failing, to solve the other 10%.

As it is, the file works – it should be bloody simple to add it to any library out there. The major problem, though, is white-space.

The innerHTML returned by IE is not exactly what it was given, but there is no other way of obtaining it.

Trust me – I spent hours crawling through the MSDN library today (in Firefox, because I had IE set on stun, so the navigation (which is ActiveX-based) wouldn’t work) – there is no other way.

I even tried dynamically creating a text/plain <object> to grab the data from it, but got alerts complaining that this was an ActiveX object!

If anyone out there in blogland has an inkling of an idea how to go forward from here, please do pipe up.


58 Comments for this entry

  • sohnee

    As iframes have been deprecated, it’s advisable not to use them.

    As fas as Active-X being disabled, it’s already been mentioned that IE7 uses the native request method.

    This still won’t work if users have javascript disabled or if they are using a mobile browser / accessability related browser.

    WIth AJAX, it really is as simple as using the normal method and having a completely non-javascript method of doing things if it’s disabled.

  • Arun

    Hi guys,
    Could someone give me a example, how to use it ?

  • CRX

    The link to the JS file is broken.

  • Arup Bhanja

    Use CPAINT API available at sourceforge.net .. it has php as well as asp code to help you at the server adn client side and works in all browsers.

    Regards,
    Arup

  • Raul

    The same problem, I’ve encountered: submitting a form into a dynamically created IFRAME: FireFox and Opera worked perfect but IE not!
    After 1 day of debugging, I founded that , following solution works:

    [CODE]
    var iFrameID = ‘ID1′;
    var myIFrame = document.createElement(‘iframe’);

    myIFrame.setAttribute(‘src’, ‘about:blank’);
    myIFrame.setAttribute(‘id’, iFrameID);
    myIFrame.setAttribute(‘NAME’, iFrameID);
    myIFrame.style.display = ‘none’;
    document.body.appendChild(myIFrame);
    if((onReadyFunction) && (typeof(onReadyFunction) == ‘function’)) captureEvent(‘load’, function(){ var iFrame = document.getElementById(iFrameID); var doc = (iFrame.contentDocument)?(iFrame.contentDocument):((iFrame.contentWindow)?(iFrame.contentWindow.document):(self.frames[iFrameID].document)); if (doc.location.href == ‘about:blank’) { return; } else { onReadyFunction(doc.body.innerHTML); } }, myIFrame);
    if(self.frames[iFrameID].name != iFrameID) { /* *** IMPORTANT: This is a BUG FIX for Internet Explorer *** */ self.frames[iFrameID].name = iFrameID; }

    [/CODE]

    Meanning that AFTER the iframe is created, it should be checked that self.frames[iFrameID].name is equal with iFrameID

    No DIV, nothing else more! That’s all! :D

    The problem was encountered and resolved by me during myAJAX [http://myajax.sf.net] development

  • tobylane

    Right… Bit insane. I’d rather ActiveX than iframes. Both by js.. so Meh.

  • Kae Verens

    tobylane, you have missed the point. The reason for this approach is not to avoid ActiveX – it’s because in some IE6 environments, ActiveX is simply not available. so you might prefer to use ActiveX, but that won’t make it work…

  • gino

    hi. i am getting this error when i incorporate nonActiveX-XMLHTTPRequest.js to my page..

    it says “req is null or not an object”

    please help :(

11 Trackbacks / Pingbacks for this entry

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Blogroll

A few highly recommended websites...