Tuesday, November 20th, 2007

HTTP Streaming and Internet Explorer

Category: Comet, IE

Michael Carter wrote about the trials and tribulations of getting HTTP streaming with IE. He knew that the htmlfile ActiveX object was the key, but kept getting errors.

Then he stumbled on the solution:

We happen to be in luck. Changing JavaScript variables, including Array functions, seems okay as far as the gods of htmlfile streaming are concerned. So our solution is to simply append event payloads to an array from within the iframe, have the parent window use use a timer loop (’setInterval’) to periodically check the array for new messages, and then pass them to the callback. It’s not as elegant as I’d like…but it beats all the other techniques I’ve tried.

Why not just call a function attached the parent window, you wonder? It turns out htmlfile’s iframe doesn’t care where the function object lives; instead, it cares which thread is used to execute the code. The htmlfile thread is a capricious beast, and will rebel when employed to do too much DOM work. The effect of setInterval is to move the actual DOM manipulations to a thread that is perfectly safe for that sort of scripting. This fix works for IE 5.01+.

Michael just put up another post that details the solution after learning about the nuances of IE. He ended up using the following code:


  1. function connect_htmlfile(url, callback) {
  2.     // no more 'var transferDoc...'
  3.     transferDoc = new ActiveXObject("htmlfile");
  4.     transferDoc.open();
  5.     transferDoc.write(
  6.         "<html><script>" +
  7.         "document.domain='" + document.domain + "';" +
  8.         "</script></html>");
  9.     transferDoc.close();
  10.     var ifrDiv = transferDoc.createElement("div");
  11.     transferDoc.body.appendChild(ifrDiv);
  12.     ifrDiv.innerHTML = "<iframe src='" + url + "'></iframe>";
  13.     transferDoc.callback = callback;
  14. }

And in the iframe:

  1. <script>parent.callback(["arbitrary", "data", ["goes", "here"]);</script>

Posted by Dion Almaer at 7:36 am
1 Comment

2.8 rating from 42 votes

1 Comment »

Comments feed TrackBack URI

That’s a indeed very interesting technique. Now the only thing missing is a solid small client library addition to handle comet loading. Maybe we’ll add something to jQuery.

Comment by Paul Bakaus — November 20, 2007

Leave a comment

You must be logged in to post a comment.