468,735 Members | 2,189 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,735 developers. It's quick & easy.

Getting scrollHeight of dynamically created iframe

There seems to be some strange behaviour when trying to get the
scrollHeight and scrollTop of an iframe in IE6.

I have tried several ways of getting these values when the iframe is
written into the html. The following return the correct values in IE6,
where the iframe's id is 'f':

f.document.body.scrollHeight
f.document.body.scrollTop
window.frames['f'].document.body.scrollHeight
window.frames['f'].document.body.scrollTop
document.frames('f').document.body.scrollHeight
document.frames('f').document.body.scrollTop

The following give a strange value for scrollHeight (definitely not
the length of the contents of the iframe) and scrollTop is always 0:

document.getElementById('f').document.body.scrollH eight
document.getElementById('f').document.body.scrollT op
document.all.f.document.body.scrollHeight
document.all.f.document.body.scrollTop

When I dynamically create the iframe, using myFrame =
document.createElement('IFRAME'), I get the same strange values as
above when I do this:

myFrame.document.body.scrollHeight
myFrame.document.body.scrollTop

However this works as correctly in Opera 7. I find this all very
confusing. Can anybody shed light onto how I might get the correct
values in IE6 when using the createElement() method?
Jul 23 '05 #1
1 23822


Martin wrote:
There seems to be some strange behaviour when trying to get the
scrollHeight and scrollTop of an iframe in IE6.
window.frames['f'].document.body.scrollHeight
window.frames['f'].document.body.scrollTop
window.frames.frameName (or window.frames['frameName']) is the best
cross-browser way to access the frame window object, and then
window.frames.frameName.document gives you the document in that frame
window.

When I dynamically create the iframe, using myFrame =
document.createElement('IFRAME'),


Make sure you give your iframe element a name and an id attribute
var iframe = document.createElement('iframe');
iframe.id = iframe.name = 'f';
then make sure you insert the iframe element into the document e.g.
document.body.appendChild(iframe);
after that you can then access
window.frames.f
respectively
window.frames.f
as the frame window.
There is a difference between the HTML <iframe> element object in the
document tree and the frame window object,
document.createElement('iframe') returns an HTMLIframeElement object
that represents the <iframe> element object in the document tree, the
same way as there are HTMLParagraphElement objects for <p> elements in
teh document tree, but an <iframe> element also necessitates the browser
to create a frame window to load the document referenced by the frame
and if you want to access that frame window cross browser then you
should use
<iframe name="f" id="f"
respectively
var iframe = document.createElement('iframe');
iframe.id = iframe.name = 'f';
to be able to script
window.frames.f
and
window.frames.f.document

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Quentin | last post: by
5 posts views Thread by Angel | last post: by
5 posts views Thread by maya | last post: by
1 post views Thread by Andrew Poulos | last post: by
12 posts views Thread by mistral | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.