1

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?

asked Apr 12, 2012 at 17:08
2
  • 3
    But 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. Commented Apr 12, 2012 at 17:11
  • @Rup You are right. So I really have an Same Origins Policy "Problem". thx Commented Apr 12, 2012 at 17:23

2 Answers 2

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.

answered Apr 12, 2012 at 17:21
Sign up to request clarification or add additional context in comments.

Comments

0

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.

answered Apr 12, 2012 at 17:17

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.