Some time ago I had posted an entry that describes how to mimic FormattedTextView control (WD ABAP) in WebDynpro for Java using IFrame. This post explains how to adjust size of IFrame control to fit size of content.
When I were writing my entry Display formatted text using WebDynpro for Java I planned myself to include some script that will automatically adjust outer frame container to fit size of inner IFrame content. However, I’d stuck with this. I could not manage to write unobtrusive code, that does not hijack internals of WebDynpro HTML client rendering. It either was necessary to use WD-generated IDs of HTML elements or accept not reliable or not generic solution.
Later I came across the following Cindy Herman. After short forum/e-mail conversation she shares her magic script with me. And this script contains really insightful thing that I’d overlooked previously: there is frameElement reference on window object, if window object is an IFrame. And this reference point to HTML element that holds frame object itself. Bingo!by
So here is first variant of script for inner IFrame. This script adjusts both width and height of IFrame container, so content is fully visible:
There are 3 important points:
- When adjusting width, we have to check both scrollWidth and offsetWidth for Internet Explorer. Also there is no explicit branching for IE, Math.max expression has effect only Redmond’s child, while in “right browsers” offsetWidth <= scrollWidth always.
- Again, our good old (very old, btw 😉 MS beast fails to render correctly from the first call. Heck knows why, probably IE performs some size calculations only when actually rendering content. Nevertheless, we just use Timeout to repeat our operation once again and everything looks ok. Hint: all events handler and rendering code are executed on same thread in every browser, so we need to use timeout to let some internal IE “draw” method execute.
- Sometimes in Mozilla/Firefox scrollbar is still drawn even if we adjust heigth to exact content height. Hence I’ve added one pixel as workaround to this silly bug.
Well, while this variant is usable, it is more common to just remove vertical scroll bar and let content flow with main window resize.
Flexible width, no vertical scrolling
Here we need to track “resize” event in browser. Also here we do not alter width in any way, so by default our content will occupy all available width of outer HTML element. What is important here?
- We need to track horizontal scroller anyway, if it is present we need to adjust total hiegth of outer container by this delta
- Now Firefox plays “interesting game” here – we need first to decrease height to some small value, and then apply “real” height. This trick is used to get correct scrollHeight when we are resizing main window from smaller size to bigger. Resizing in opposite direction works without this trick.
Download JAR archive file to get working examples of both techniques
Btw, two words to those who are visiting SDN World:
- Thank you! To all of 600 users registered as at time of writing this post.