开发者

GWT - break out of an iframe

My GWT app is being a开发者_运维问答dded to an iframe. I have a button in the app which then should direct to another page. Currently, I'm doing it like this:

Window.Location.assign("http://testpage.com");

The problem right now is that new page is being loaded into the iframe. Is there a way to reload the whole page to avoid having that effect?

I have read some posts here on stackoverflow but none of them where in GWT. I hope someone already had the problem and solved it successfully.

Thanks for any advices!


I don't believe GWT has a built in function you can call for this.

The usual idiom in JavaScript to do this looks like this

if (top.location!= self.location) {    //test if in an iframe
    top.location = self.location.href; //if so, break out
}

This won't work directly in GWT, though you could put it in your main html page, probably in the <head> before the rest of the app loads - this will kick you out of the iframe right away instead of waiting for the app to load, breaking out, then waiting for the app to load again.

If you really want to run the check in GWT, perhaps because you only sometimes want to break out, you'll need a very short JSNI method to add this feature, something like this:

public native void breakOut() /*-{
    if ($wnd.top.location != $wnd.location) {
        $wnd.top.location = $wnd.location.href;
    }
}-*/

Note the use of $wnd to refer to the app's window object. See http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html for more details on writing JSNI. Do not expect this function to return - it is triggering the browser to unload this page and load another.


Here is a customized version of assign based on the idea Colin mentions that should do what you intend, it will also work if you're not in an iframe:

public static native void assignTop(String newURL) /*-{
    $wnd.top.location.assign(newUrl);
}-*/

EDIT: simplified implementation based on Colin's suggestion. thanks!


You can use Location.replace("http://testpage.com"); to redirect the browser to a different URL with GWT.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜