process external text using iframe

Standard browser scripts are very restricted as to their input: they mostly operate on the HTML in the current web page, and have no native means of accessing other data. HTML itself however has a couple of mechanisms for loading external documents, which can be used as proxies for a script.

This loads text from an external file into an invisible iframe object, then with JavaScript gets the text from the object, processess it, and displays it in a different part of the web page. HTML could also be similarly obtained as text or traversed as an object tree.


document load sequence

At the time the main document is loaded, any child iframe referring to an external document is empty. This is because if the external document takes time to download, the rest of the document should be rendered first.

The onload attribute of the iframe can be set to execute a function when the document arrives. So the trick is to set a timer to wait until this event fires, then to get and render the iframe contents.


Doesn’t support all the DOM calls one would like, but there are work-arounds.

Worse: if one tries to load a file of type text/javascript, it wants to “execute” the poor thing. It makes some sense, insasmuch as the original idea of the iframe was to display a secondary web page in a sub-frame. But one would like to turn that off somehow. In other browsers, non-HTML is simply wrapped in PRE tags.


Version 3.x assumes that if the display style of the iframe is set to none, then we didn’t want the contents anyway, so it goes ahead and fires the onload event, but doesn’t provide access to the contents (contentDocument of object is null).

Reported: Konqueror bug #141787.

the result:

further problems

If source document is typed as JavaScript, and iframe is visible, asks if it’s OK to display javascript, and colorizes it in the iframe with Kate! (which defeats my purpose)

But in JS case seems also not to trigger onload().

Konqueror’s pathology is triggered when the included document is type text/javascript.

All text and buttons following the iframe are lost, even when the main script works, or even if it has been commented out! Mea culpa: I was using a self-terminating iframe tag, which it grokked not.

Curious: it works when the included document is HTML, fails when it is plain text or JavaScript. Seems it happens precisely when I get a dialog asking whether I want to load the document or not.

Well, this is typical. MSIE is trying to *execute* the poor text file. Can I make it stop? Is there a “stopBeingStupid” method?

But seems also not to trigger onload().