iframe change listener?

8,084 views
Skip to first unread message

aarnott

unread,
Jan 15, 2008, 4:21:06 PM1/15/08
to Google Web Toolkit
I would like my application to include an iframe and I would like to
be able to detect when a link has been clicked in that iframe (and
also get information about the new page). For example, supposing the
iframe points to http://code.google.com/webtoolkit/, I would like to
be able to detect when the user clicks a link and then be able to read
the new page and make the text between the <title> tags be the content
in a lower panel of my application.

To set the iframe, I am using the following lines of code:

HTML data = new HTML("<iframe src='" + URL + "' style='width:100%;
height:100%;'></iframe>");
panel.add(data);

where URL is the url of the page.

I have tried using a click listener:

data.addClickListener(new ClickListener (){

public void onClick(Widget w) {
System.out.print("click");
}
});

But nothing is printed to the console. How can I make this work? Am
I using the correct approach?

Thanks,

Andrew

Ian Bambury

unread,
Jan 15, 2008, 6:13:09 PM1/15/08
to Google-We...@googlegroups.com
Hi Andrew,


On 15/01/2008, aarnott <Andrew.W...@gmail.com> wrote:
Am I using the correct approach?
 
Not really. You are listening in one DOM for an event happening in another DOM.
 
Getting from the IFRAME to the parent by JS is fairly simple but getting to the contents of the IFRAME *from* the parent has all sorts of security implications which vary between browsers, as I remember. If you can link into the DOM of a child IFRAME and put listeners in, then you can collect keypresses and therefore passwords and userIDs.
 
Since this is not possible in pure GWT but needs JSNI, then you might be better off in a JavaScript group.

 
 
 

aarnott

unread,
Jan 16, 2008, 12:43:32 PM1/16/08
to Google Web Toolkit
Thanks for the quick response! I actually don't have to worry about
security because what I am linking to is hosted locally. All I needed
to get was the url of the iframe at its current state and also the
page title.

You have helped me a lot -- I now am using JSNI to get this
information. I am having problems with one simple thing however. I
cannot seem to pass the javascript strings back to my Java code (so I
don't think that would be a question for javascript forums). Here are
the relevant functions I am using:

public void testJS(String URL) {
System.out.println("URL: " + URL.toString());
}

public native void setupJS(PageViewer x) /*-{

$wnd.iframeLoad = function (iframe) {
var content;
//contentDocument is Firefox, contentWindow is IE
if(iframe.contentDocument != undefined) {
content = iframe.contentDocument;
} else {
content = iframe.contentWindow;
}
var url = content.location;
var title = content.document.title;

alert(url);
alert(title);

//This line does not work!
x.@com.aarnott.client.PageViewer::testJS(Ljava/lang/String;)(url);
};
}-*/;

The iframe has the attribute: onload='iframeLoad(this);' --> I know
that works because I can alert the URL and title on page change.

The instance of PageViewer x does work because I tested an empty
function called testJS2:

public void testJS2() {
System.out.println("got here...");
}

And it worked with the line:
x.@com.aarnott.client.PageViewer::testJS2()();

Any ideas why my testJS function does not work? It obviously has
something to do with passing parameters, but I think I am following
the JSNI instructions correctly (from
http://code.google.com/webtoolkit/documentation/com.google.gwt.doc.DeveloperGuide.JavaScriptNativeInterface.html).

Thanks,

Andrew

On Jan 15, 6:13 pm, "Ian Bambury" <ianbamb...@gmail.com> wrote:
> Hi Andrew,
>

aarnott

unread,
Jan 16, 2008, 12:52:09 PM1/16/08
to Google Web Toolkit
Nevermind... It ended up being really stupid.

document.location is an object.

document.location.href is a string.

That's all I needed to change.

Andrew

Ed

unread,
Mar 11, 2008, 1:31:35 AM3/11/08
to Google Web Toolkit
Can you post the example that you had? I am trying to follow but I
cannot get the result that you've got.

You mentioned that you have the onload() method, this is what I have
private Widget createIFrame() {
HTML c = new HTML("<iframe name=\"myIframe\" id=\"myIframe\"
src=\""+ url +"\" frameborder=\"0\" height=\"1300\" width=\"100%\"
onload=\"iframeLoad(this);\"></iframe>");
return c;
}


This is called in onModuleLoad() to get the HTML widget, then I have:

public native void setupJS(MyClass x) /*-{

$wnd.iframeLoad = function (iframe) {
var content;
//contentDocument is Firefox, contentWindow is IE
if(iframe.contentDocument != undefined) {
content = iframe.contentDocument;
} else {
content = iframe.contentWindow;
}
alert("abc2");
var doc = content.document;
alert("abc3");
var url = content.document.href;

alert(url);
};
}-*/;

I am able to get abc2 alert but not abc3 alert. I assume my content
is null. Do you know what is the difference between my code and your
working code?

ThomasDalla

unread,
Mar 15, 2012, 2:30:16 AM3/15/12
to google-we...@googlegroups.com, Google Web Toolkit
Hi,

Why not using the LoadHandler:

        Frame f = new Frame();
        f.addLoadHandler(new LoadHandler() {

            @Override
            public void onLoad(LoadEvent event) {
                _logger.info("LoadEvent: " + event.toString() + "\n" + 
                             "New URL: " + f.getUrl() + "\n" + 
                             "Relative Element: " + event.getRelativeElement());
            }
            
        });

It doesn't catch the POST parameters, but JS doesn't neither, isn't it?

Cheers,

Thomas
Reply all
Reply to author
Forward
0 new messages