2012-02-24 63 views
3

我有一个GWT应用程序,它有一个框架指向其他网站的URL,像这样:frame.setUrl(other web url);使用GWT访问框架的文档

但有时可能会出现其他网址错误,说网络错误,或网页不可用,我需要解析出这些错误,并提出一个用户友好的错误消息在我的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; 
    }-*/; 

它总是返回文档为空,我想这是因为相同的政策。所以我如何访问GWT中的框架文档?谢谢

回答

1

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