By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,616 Members | 1,198 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,616 IT Pros & Developers. It's quick & easy.

Getting scrollHeight of dynamically created iframe

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a


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.