how to get elements in iframe

1851 views
Skip to first unread message

jinqi.yin

unread,
Sep 7, 2007, 2:36:55 AM9/7/07
to greasemonkey-users
i try to get elements in iframe , it doesn't work.
the code seems like this .

function outlinksframe(){
if(!document.getElementsByTagName("iframe")) return;
var iframes = document.getElementsByTagName("iframe");
for(var j=0; j<iframes.length; j++){
if(iframes[j]){
if(iframes[j].getElementsByTagName("form")){
var form11 = iframes[j].getElementsByTagName("form");
form11.action = "";
form11.target = "_blank";
}
}
}
}
outlinksframe();


yours
jinqi

Vectorspace

unread,
Sep 7, 2007, 10:47:41 AM9/7/07
to greasemon...@googlegroups.com
You can only access the contents of a frame if the frame is from the
same domain as the parent page.

E.g. If the page is www.google.com/ and the frame is
www.google.com/preferences then it will work.
If the page is www.google.com and the frame is www.microsoft.com then it
won't work.

Cross-site scripting (javascript on one page accessing the contents of
another page on a different domain) is disabled in Firefox for security.

brosel

unread,
Sep 7, 2007, 10:36:21 PM9/7/07
to greasemonkey-users
Hi!
search the goups for
IFRAME cross site access
some people gave me great hints about how to do such a things (there
are in fact several workarounds)

brosel

jinqi.yin

unread,
Sep 9, 2007, 10:41:53 PM9/9/07
to greasemonkey-users
Hi Vectorspace
yes ,i access the same domain as the parent page.

but i found that i use the wrong property ,'contentDocument' is the
right property of iframe. after changed it to this correct property,
i get the iframe content. but a new problem arise , that is before
all of the contents of iframe loaded in , the g.m. already finished.
that means i could not get all the contents of the iframe by the
g.m.

i think there will be at least 1 method to solve this,
set a timer to control the g.m. script to run.
but , an undefined function error occured .
is that mean g.m. only run at once the page loaded in?

thanks
jinqi

On Sep 7, 10:47 pm, Vectorspace <vectorsp...@ntlworld.com> wrote:
> You can only access the contents of a frame if the frame is from the
> same domain as the parent page.
>

> E.g. If the page iswww.google.com/and the frame iswww.google.com/preferencesthen it will work.
> If the page iswww.google.comand the frame iswww.microsoft.comthen it

Vectorspace

unread,
Sep 10, 2007, 2:12:49 AM9/10/07
to greasemon...@googlegroups.com
contentDocument is limited, in my experience. But you were half way
there already.

You were using frames[j].getElementsByTagName(...)

As if the frame object was the equivalent of the document object - it is
not. The frame object is the equivalent of the window object.

So, frames[j].document.getElementsByTagName(...) should work.

jinqi.yin

unread,
Sep 10, 2007, 2:13:11 AM9/10/07
to greasemonkey-users
hi brosel
thank you for your advice.
and i saw the topic you said.
it is useful.

jinqi

jinqi.yin

unread,
Sep 10, 2007, 2:30:09 AM9/10/07
to greasemonkey-users
hi Vectorspace

> So, frames[j].document.getElementsByTagName(...) should work.

i dosenot work in my browser .


jinqi

> >> E.g. If the page iswww.google.com/andthe frame iswww.google.com/preferencesthenit will work.

jinqi.yin

unread,
Sep 10, 2007, 3:07:43 AM9/10/07
to greasemonkey-users
Thanks for all your help

it is now worked.
after read the article of the pitfall ,[1]
i modified my code .
i changed the code from
setTimeout('outlinksframe()',3000);
to
setTimeout(outlinksframe,3000);

thanks to every one in this group.

[1] article: http://www.oreillynet.com/lpt/a/6257

> > >> If the page iswww.google.comandtheframe iswww.microsoft.comthenit

Reply all
Reply to author
Forward
0 new messages