Get dom of a iframe

695 views
Skip to first unread message

julio

unread,
Jul 7, 2007, 10:50:57 AM7/7/07
to Google Web Toolkit
Hi,

I have found this sample on internet:

file 1 (main.html):

<html>
<head>
<title></title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
function IFTags(id){
var mmspobj=document.getElementById(id);
var mmsiobj;
if (mmspobj.tagName!='IFRAME'){ return; }

mmsiobj=window.frames[id].document.getElementsByTagName('BODY')[0];
var remove=mmsiobj.getElementsByTagName('H2');
var ary=[];
for (var zxc0=0;zxc0<remove.length;zxc0++){
ary.push(remove[zxc0]);
}
for (var zxc0=0;zxc0<ary.length;zxc0++){
mmsiobj.removeChild(ary[zxc0]);
}
}
//-->
</script>
<iframe name="MFrame1" id="MFrame1" src="x1.html"
style="position:relative;top:10px;left:0px;"
width="500" height="200" frameborder="1" marginwidth="0"
marginheight="0" wrap="off"
>
</iframe>
<input type="button" name="" value="Remove h1"
onclick="IFTags('MFrame1')";>
</body>
</html>


and file 2 x1.html:

<html>
<head>
<title></title>
</head>
<body>
<h1>test-row1</h1>
another row
<h1>test-row2</h1>
</body>
</html>

I would like "transform" it in gwt-code. But it drives me crazy:) cos
I cannot get DOM of iframe-content. Expecially I suppose is critic
this javascript line above:

mmsiobj=window.frames[id].document.getElementsByTagName('BODY')[0];

I tried to do a "my own frame" to "catch with mouse over" single
element, but it doesn't work

public class SuperFrame extends NamedFrame {

private Label label;

public SuperFrame(String name) {
super(name);
}

public SuperFrame(String name, String url, Label label) {

super(name);
super.setUrl(url);
this.sinkEvents(Event.ONMOUSEOVER);// | Event.ONMOUSEOUT);
this.label = label;
}

public void onBrowserEvent (Event event) {
super.onBrowserEvent(event);
int type = DOM.eventGetType(event);
switch (type) {
case Event.ONMOUSEOVER: {
Element parent = getElement();
for (int i=0; i<DOM.getChildCount(parent); i++) {
System.out.println ("Try to read something: " +
DOM.getChild(parent, i).toString());
}
break;
}
}
}

}


Have you any idea how can I get and modify a iframe-content with
GWT???

Thanks,
Julio

antonio giulio

unread,
Jul 9, 2007, 7:29:10 AM7/9/07
to Google Web Toolkit
And so, is not possible get elements via DOM in a "iframe" with GWT?
Is there anyway? Or the only way is use directly javascript?

Thanks,
Julio

Reinier Zwitserloot

unread,
Jul 9, 2007, 7:48:09 AM7/9/07
to Google Web Toolkit
Yes, you'll have to use JSNI.

George Georgovassilis

unread,
Jul 9, 2007, 8:59:34 AM7/9/07
to Google Web Toolkit
Hello Julio

What you're asking is easy albeit with occasional pit falls - if you
search the web you'll find that accessing the content of an iframe or
even finding it at all depends on the browser and how you do it [1].
In the (unpublished) GWT-SL svn trunk I am using this technique to
parse the contents of an iframe using JSNI, for a concrete example you
can lookup [2]


[1] http://www.quirksmode.org/js/iframe.html
[2]
http://gwt-widget.svn.sourceforge.net/viewvc/gwt-widget/server/trunk/src/test/java/org/gwtwidgets/server/spring/test/client/ProgressFrame.java?view=markup

antonio giulio

unread,
Jul 9, 2007, 9:46:04 AM7/9/07
to Google-We...@googlegroups.com
Nice! It would be perfect cos I'm using gwt-library to integrate gwt in spring:)

Expecially this seems interesting:

33 private native JavaScriptObject getFrameDocument()/*-{
34 var frameId =
this.@org.gwtwidgets.server.spring.test.client.ProgressFrame::frameId;
35 var f = $wnd.frames[frameId];
36 try{
37 return f.document;
38 }
39 catch (e){
40 // produces an access exception when frame has not loaded yet
41 return null;
42 }
43 }-*/;

but that jsni-code is not "portable"? that's, it works only on firefox
and not on explorer (I believe)

Thanks,

George Georgovassilis

unread,
Jul 9, 2007, 9:59:34 AM7/9/07
to Google-We...@googlegroups.com

but that jsni-code is not "portable"? that's, it works only on firefox
and not on explorer (I believe)

Worked nice for me on IE7, FF and Opera

Reply all
Reply to author
Forward
0 new messages