Reputation: 91
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.
Upvotes: 1
Views: 203
Reputation: 3083
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.
Upvotes: 0
Reputation: 28114
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).
Upvotes: 0