2011-10-17 14 views
1

私は2つのフレーム(com.google.gwt.user.client.ui.Frame)を特徴とするGWTアプリケーションを持っています。 Via Frame.setUrl(...)問題なく任意のWebページを読み込むことができます。もちろん、ユーザーは読み込まれたページのリンクをクリックすることができます。両方のフレームで現在ロードされているページを追跡するにはどうすればよいですか?GWT:フレームの現在のURL

以下は、2種類のリスナーを使用した現在の試みです。コードスニペットがWeb上に見つかりました。どちらのイベントが発火、それでも私は、現在ロードされたURL

import com.google.gwt.event.dom.client.LoadEvent; 
import com.google.gwt.event.dom.client.LoadHandler; 
import com.google.gwt.user.client.DOM; 
import com.google.gwt.user.client.Event; 
import com.google.gwt.user.client.EventListener; 
import com.google.gwt.user.client.ui.Frame; 

public class BrowserTabFrame extends Frame implements EventListener { 

public BrowserTabFrame() { 
    super(); 
    sinkEvents(Event.ONLOAD); 

    addLoadHandler(new LoadHandler() {  
     @Override 
     public void onLoad(LoadEvent event) { 
      System.out.println(event.getSource()); 
      // <iframe style="visibility: visible;" id="ext-gen17" src="http://..." class="gwt-Frame"></iframe> 
      // however, the src attribute never changes 
     } 
    }); 
    } 

    @Override 
    public void onBrowserEvent(Event event) { 
    super.onBrowserEvent(event); 
    if (DOM.eventGetType(event) == Event.ONLOAD) 
     System.out.println(event.getCurrentEventTarget()); 
     // [object HTMLIFrameElement] 
     // no idea what to do with it 
    } 
} 
任意のヒントについて

感謝を取得する方法がわかりません!

クリスチャン

答えて

2

iframesrc属性が変化しないが含まれているdocument意志のURL財産ありません。あなたはJSNIを使用してこの値を取得することができます

private native String getIframeUrl(IFrameElement frame) /*-{ 
    if (frame.contentDocument !== undefined) { 
    return frame.contentDocument.URL; 
    } else if (frame.contentWindow !== undefined && 
      frame.contentWindow.document !== undefined) 
    { 
    return frame.contentWindow.document; 
    } else { 
    return null; 
    } 
}-*/; 

... 

Window.alert(getIframeUrl(myFrame.getElement())); 

しかしiframe変更のセキュリティコンテキスト(プロトコル、ドメインまたはポート番号のいずれかの)場合は、ブラウザが含まれているアプリケーションがdocumentにアクセスすることを許可しないだろう、ということに注意してくださいフレームの中。

+0

こんにちは、ジェイソン!お返事をありがとうございます!これはまさに私が探していた情報でした。そして、私はこのセキュリティ上の問題にぶつかります。 GWTアプリケーション内で最小限のWebブラウザ機能を実現するための代替/ベストプラクティスのテクニックはありますか?基本的に十分なIFrame内でクリックされたリンクを把握できれば再度、感謝します! Christian – Christian

+1

いいえ、Javascriptだけを使用してこの制限を回避する方法はありません。 –

1

これは上記のJasonのソリューションの拡張です。私がJasonのコードを試してみたところ、返されたURIにはハッシュ(フラグメント、別名「#」で始まるURIサフィックス)がありませんでした。 DOM仕様の周りにいくつかの掘削後、働い以下:

ジェイソンのコードに加えるだけで、ネストされた状態である
public static native String getIframeUri(IFrameElement iframe) /*-{ 
    if (iframe.contentDocument !== undefined) { 
     if (iframe.contentDocument.defaultView !== undefined 
       && iframe.contentDocument.defaultView.location !== undefined) { 
      return iframe.contentDocument.defaultView.location.href; 
     } else { 
      return iframe.contentDocument.URL; 
     } 
    } else if (iframe.contentWindow !== undefined 
      && iframe.contentWindow.document !== undefined) { 
     return iframe.contentWindow.document; 
    } else { 
     return null; 
    } 
}-*/; 

if (iframe.contentDocument.defaultView !== undefined 
     && iframe.contentDocument.defaultView.location !== undefined) { 
    return iframe.contentDocument.defaultView.location.href; 
関連する問題