使用GWT访问框架的文档

时间:2012-02-24 16:59:26

标签: gwt

我有一个GWT应用程序,它有一个指向其他网址的框架,如下所示:frame.setUrl(其他网址);

但有时可能会出现其他网址的错误,比如网络错误或网页不可用,我需要解析这些错误并在我的GWT应用程序中显示用户友好的错误消息,但无法弄清楚如何做那个框架,我试过:

frame.addDomHandler(new LoadHandler() {

            @Override
            public void onLoad(LoadEvent event) {
                Window.alert("loaded!");

                IFrameElement iframe = IFrameElement.as(frame.getElement());
                Document frameDocument = getIFrameDocument(iframe);
                if (frameDocument != null) {
                    Window.alert(frameDocument.getDomain());
                }
                else {
                    Window.alert("the document is empty, nothing to display!");
                }

            }
        }, LoadEvent.getType());
    }

    private native Document getIFrameDocument(IFrameElement iframe) /*-{
        return iframe.contentDocument;
    }-*/;

它始终将文档返回为null,我想这是因为同一个orign策略。那么如何在GWT中访问框架文档?谢谢

1 个答案:

答案 0 :(得分:1)

请尝试在GWT提供的getIFrameDocument()上调用iframe.getContentDocument(),而不是使用原生的IFrameElement JSNI方法。这是the javadoc for that method