1

I read that the following should work in order to access the contents of an iframe with javascript:

document.getElementById("iframe_name").contentDocument

or

document.getElementyById("iframe_name").contentWindow.document

However, neither of those methods work for me. The iframe I am trying to access is a Google form that I created. It is a registration form that I created for my website. I want to do some calculation on some of the fields that the user types in then display it after the iframe. Accessing those fields using javascript is the best way I could think of. Why can't I access it? Is there maybe a better way to achieve my goal? Thank you.

1
  • This looks correct. What is telling you that these methods are not working? Commented Sep 29, 2012 at 21:37

3 Answers 3

1

You can't acces external page like google.com from your server.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, I guess this is it. Thanks. Anyway, what I did in the end is simply copy the code inside the iframe (which is simply a Google form) and then I could both manipulate it however I want and I could also access it. In the end, all there is to it is the POST request that it does (to a certain URL that Google creates for the form). Thanks everyone.
0

What you have is correct. Here is a more complete sequence that should work cross-browser:

var ifr=document.getElementById("iframe_name");
var doc=ifr.contentWindow||ifr.contentDocument;
if (doc.document) doc=doc.document;

Update: as said in another answer, this won't work across domains (for example if you try to access google.com from mydomain.com).

Comments

0

I don't know what you wishing to do but maybe this will help you..

I used this to reload the frame

document.getElementById('graphFrame').contentDocument.location.reload(true);

you can see that the location attribute is related to the iframe.

Next, I am changing a div inside an Iframe like this.

top.frames["graphFrame"].document.getElementById('right_example');

So the top.frames is array of the page frames. then i am searching for a dom element which his ID is "right_example" then you can do what ever you want with that.

Comments

Your Answer

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.