user_1357
user_1357

Reputation: 7940

How to access codes outside of IFRAME in GWT?

I have a html, say A.html. I am invoking A.html inside of A.html inside an IFRAME.

A.html
<html>
.....
  <IFRAME>
    A.html (same domain, etc as A.html)
  </IFRAME>
</html>

This is a GWT app. This IFRAME is a popup, and when the user closes (clicks a close button) this popup (which is also loading the A.html as you can see above), I want to fire an event to display some message on the outer A.html which means I need an access to codes outside of IFRAME. How can I achieve this?

Thanks

Upvotes: 0

Views: 1458

Answers (2)

user_1357
user_1357

Reputation: 7940

IFRAME in the GWT module is defined as:

<iframe src="/temp.jsp"></iframe>

GWT module (Outer class):

  public class Test implements EntryPoint {

        public void onModuleLoad() {
              registerMethod();   
        }

        private static native void registerMethod() /*-{
              $wnd.mt = $entry(@test.client.Test::show(Ljava/lang/String;));
            }-*/;

        public static void show(String value){
              Window.alert("Calling from inside IFRAME " + value);
        }  
    }

JSP inside the IFRAME: temp.jsp

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>temp</title>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <style type="text/css">
    </style>
    </head>
    <body>
        <div id="content">
          Holla commo estas from the iframe!    
        </div>

        <script type="text/javascript">
        window.alert("inside iframe");
        window.parent.mt("passing a value to a parent!!!");
        </script>
    </body>
</html>

Upvotes: 0

pb2q
pb2q

Reputation: 59617

Since your iframe contents are being loaded from the same domain, you can call javascript functions on the containing page. I'm assuming that you're asking about calling the code outside the iframe from GWT. If so, then you can use JSNI.

For example, given the JSNI method:

private final native void postClosedEvent()
/*-{
    $wnd.functionDefinedOutside();
}-*/    

You could call the method within a GWT ClickHandler:

closeButton.addClickHandler(new ClickHandler()
                            {
                                public void onClick(ClickEvent e)
                                {
                                    postClosedEvent()
                                }
                            });

This assumes a function defined using javascript in the containing page, for instance:

<script type="text/javascript">
    var functionDefinedOutside =
        function()
        {
            alert("GOT MESSAGE FROM iframe");
        };
</script>

Note: If the iframe contents are loaded from a different domain, you can achieve the same result using postMessage.

Upvotes: 1

Related Questions