[Javascript] html in html display

Lester lester at denhaag.org
Thu Nov 7 22:19:30 CST 2002


Dear Andrzej,

On 11/03/02 05:38, =?ISO-8859-2?Q?Andrzej_Wróblewski?= wrote:
> I have a html document and in this document I need to 
> display another html doc. How to do this ???
> I try document.open(url="doc.html"); but it won't work

Assuming that you want to to keep the original document content there is a 
solution, albeit one that is quite convoluted.

You add as many DIVs as you want documents to insert, either hard coded or using 
doc.write statements. You also need an iFrame in the document which has its 
'display' attribute set to 'none' in a style sheet.

You load the desired HTML file into the iFrame with Javascript, using its 'src' 
attribute ["document.getElementById('<IFRAMEid>').src = 'doc.html'"] and then 
transfer the HTML to the DIV reading out the iFrame's 'innerHTML' property and 
feeding it to the DIVs 'innerHTML' property.

The DIV will size itself according to its contents so you won't have to worry 
about scrollbars and your added HTML will be visible in the browser window 
without erasing what's there already.

Where it gets complicated is that apparently there's a bug in Mozilla/Netscape6+ 
that prevents the immediate transfer from the iFrame to the DIV so you can't do 
both the loading and the transfer from the Javascript functions. The workaround 
is to have the transfer done from the iFrame document's 'onload' handler, i.e. 
each separate HTML document has an onload handler which says

"parent.document.getElementById('<DIVid>').innerHTML = document.body.innerHTML"

Be aware that this approach does not work in NN4x since it can't reflow content. 
Also I have only tested this in Mozilla so YMMV. Hope it helps anyway.

If instead you want to *replace* your original content, the syntax you used 
[document.open(url="doc.html")] is incorrect and should be "document.location = 
'doc.html'" but, as I said, this would erase the original contents of the window.

If anything is unclear or you run into problems don't hesitate to ask.

l8R lES
-- 
What would the future be like without Mozilla? There's the frightening risk that
m$ Internet Explorer would become the Mr. Burns of our online Springfield.
                                                  Andy Ihnatko, Chicago Sun-Times
http://open.denhaag.nu                                    http://www.denhaag.org




More information about the Javascript mailing list