Accessing the browser window object

507 views
Skip to first unread message

Sumit Chandel

unread,
Mar 20, 2008, 2:21:43 PM3/20/08
to Google-We...@googlegroups.com


On Thu, Mar 20, 2008 at 2:20 PM, Sumit Chandel <sumitc...@google.com> wrote:
Hi anceaux,

I think the solution I've proposed below should work to accomplish what you're looking to do. But before considering the solution, are you sure that you want to scroll your application into view within a container page?

If I understand how your application is setup in the container page, I would compare it to something like a Google Gadget, where the Gadget container page contains a bunch of different gadgets. It seems wrong to force the Gadget container page to scroll in a specific gadget on the page. You may want to take this into consideration before getting the container page to scroll your specific part of the application into view, but if it seems appropriate for you to do something like that in your specific case, then the solution below might work.

The reason why the DOM.scrollIntoView(DOM.getElementById("_sort")); call isn't working when your application is in the container page your mentioning is because of:

1) The way a GWT application loads up in a page
2) The fact that your GWT application is contained in an iframe within the page

The way a GWT application loads in a page is by creating an iframe for your application and then injecting the application code into the iframe. Just before injecting the application code, the $wnd variable is bound to the parent window that contains the iframe, which in the normal case is the top-level parent window object.

Because your GWT application is contained in an iframe defined on the container page, the $wnd variable now points to the iframe rather than the parent window object. This means that any references to $wnd will now be made against the iframe window object instead of the parent window object. Looking into the DOMImpl.scrollIntoView code, you'll notice that the loop keeps bubbling up from the current element that you want to scroll on up to its parent (which typically would be the iframe used to inject the GWT application code). Once it reaches the body of that iframe, it sets the scroll position accordingly.

In your case, you want the scrollIntoView code to bubble up to the container page's iframe that is holding your GWT application. A solution that _might_ work is to create a JSNI method with a variation of the scrollIntoView code such that when the curNode is null (because it hit the GWT constructed iframe's body), you could explicitly make it bubble up to the container page's iframe and get its offsets, and then bubble up to the container page itself and set its scroll position accordingly.

I would agree with you that this seems a bit painful, but given the constraints of your application setup it's the first solution that comes to mind.


Hope that helps,
-Sumit Chandel

On Mon, Mar 17, 2008 at 9:54 PM, anceaux <krob...@qwest.net> wrote:

On Mar 14, 2:30 pm, "Sumit Chandel" <sumitchan...@google.com> wrote:
> Hi dolcraith,
>
> Could you elaborate a little more on what you're trying to do? GWT
> applications themselves are loaded in iframes, and allow you access to the
> parent document's `window` variable through the Window class.
>

I am having similar difficulties. I have a GWT app which is loaded
from a server I do not control.
My gwt app is invoked in an iframe and appears correctly amongst other
page elements -
I do not need to mess with ( the client has control of them ).

 My app is a fairly complex item that does a bunch of paging from the
results of RPC calls.
 One page can only display 12 results ( 3 rows x 4 cols ) and
selecting backward <-> fwd
is achieved by hitting some buttons along the bottom of the set, below
the 3rd row.

The prob is that on each refresh I need to scroll the containing page
up a bit
 to display the uppermost (4) items instead of just sitting where it
is. The entire page is vertically
longer than most windows display anyway, and my app is 1200px alone.
As you mention below, 'Window.xyz' refers to that of my iframe, not
the container.

So I have experimented with the JSNI solution:

public static native void alert() /*-{
 $wnd.alert($wnd.outerHeight);
}-*/;

which does indeed seem to give the correct height of the containing
window,
 which tells me I must be referencing the proper object. One would
think I
could also influence the scroll position of that object.
 However, I cannot get that window to scroll in any way.

I have seen some references in the group to setting style
overflow={scroll, auto} on the body of the containing doc.
Have not had joy with that ( i can alter the html the client
provides ).

 Delivering the app by itself ( not in an enclosing iframe ) works
great and the solution is easy -
 by invoking some DOM / Element action and setting the Id Attribute =
'_sort' on one of the items at the top of the list:

       DOM.scrollIntoView(DOM.getElementById("_sort"));


I hope that explanation is in clear and on topic.
thanks,
anceaux



> Alternatively, you can access the `window` variable using JSNI (JavaScript
> Native Interface, link below). In your JSNI code, the `$wnd` variable is
> mapped to the parent document's `window` variable. Referring to `window` in
> your JSNI code will use the window variable local to the iframe in which the
> GWT application is loaded.
>
> JavaScript Native Interface:http://code.google.com/webtoolkit/documentation/com.google.gwt.doc.De...
>
> Do any of the above accesses to various `windows` satisfy what you're trying
> to do? If not, please post up more specific on what you're trying to
> accomplish.
>
> Hope that helps,
> -Sumit Chandel
>
> On Thu, Mar 13, 2008 at 12:51 PM, dolcra...@gmail.com <dolcra...@gmail.com>
> wrote:
>
>
>
> > Is there any way of accessing the Browser/Window/Document from within
> > an iFrame in gwt (not native js)?




Reply all
Reply to author
Forward
0 new messages