Is it possible to check whether an element exists in an iframe or not? I know it is impossible to manipulate something on a foreign host because of the Same Origin Policy. But what if I just want to find out if an element exists?
<body id="parent">
<iframe src="foreign host">
<span id="exist">
Yes, Yes. I'am here.
</span>
</iframe>
</body>
I tried to figure this out with jquery:
$('iframe:first').contents().find('#exist').length throws out a
Permission denied to access property 'ownerDocument' error
I do not wanna change something in the foreign document. So there is no danger. Why is this denied?
Is there another way to do this?
-
3But with read-only access you could e.g. throw up a phishing site which loaded my online bank in an iframe then read all my bank details out of the frame as I used it.Rup– Rup2012年04月12日 17:11:05 +00:00Commented Apr 12, 2012 at 17:11
-
@Rup You are right. So I really have an Same Origins Policy "Problem". thxs3mre8– s3mre82012年04月12日 17:23:05 +00:00Commented Apr 12, 2012 at 17:23
2 Answers 2
Sorry but the Same Origin Policy does not allow it because you can read personal information on another domain or do a phising just embeding a full document iframe.
There are alternative solutions:
- Run the browser without security: p.e.: 'chromium-browser --disable-web-security'
- Make a proxy and do the http connection like a client. Make the proxy in the same domain as your page.
If it's in the same domain any other comments can help you.
Comments
You should assign an ID to your iframe tag like so:
<iframe src="blah.html" id="blah"></iframe>
You can then reference that iframe in jQuery by doing:
var iframe = $("#blah");
Once you've done that you can use the contents() function on the iframe var you just created to access its DOM.