buy my book!

AJAX in IE without ActiveX

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.

69 comments to AJAX in IE without ActiveX

  • Hadar

    I was a little bit hasty.
    formObj.innerText does not work well.
    I’ll try to figure how to retrieve the response…

  • Rod

    I am a chinese,I want to know more about Ajax.

  • Rod

    How to use it , will you give me a example?
    My QQ 281351519
    Sincerely

  • I really don’t think it’s worth while going back to using iframe, it’s better off to simply incourage use of newer browsers or simply use regular static pages. The loading time is just not worth the difference in usability.

  • Gus

    Hi Hadar,
    how do you pass parameters using a form dynamically?

  • Hey Kae, I haven’t read through all of the comments so I’m not sure if you’ve solved the problem or not, but I just wanted to let you know this is a fantastic approach. Genius scripting. I wanted to let you know this works perfect with ColdFusion MX7.

    Jake

  • Thanks, Jake. Always feels good to be appreciated!

  • web

    Late to the party.

    But, all you need is to insert a script tag on the calling page. Then the target url of the script tag should generate just:

    var foo=bar;
    .
    .
    .
    var zoo=whatever;

    The url of course can be something like foobar.js?p=23&v=blah
    The server can of course return the unique value of foobar

    Been doing it this way since 2000AD with no problems. No need for eval(), so it is
    about as fast as you can get. The vars exist on their own as soon as received.

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

  • 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

  • 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

  • [...] klog » Blog Archive » AJAX in IE without ActiveX (tags: Ajax) [...]

  • tobylane

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

  • 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 :(

  • [...] reports: When IE is set to disallow safe Active-X, AJAX applications will not work. So, I tried to replicate what might be going on – I set my own Internet Explorer to stun – High [...]

  • [...] Internet Explorer 6 ma k?opoty z Ajaksem, kiedy nie ma w??czonej technologii ActiveX, cho? mo?na temu zaradzi?. [...]