2

I want to get an element from an iframe using JavaScript. I've tried the following:

var iframe = document.getElementById("iframe id");

var iframeWindow = iframe.contentWindow || iframe.contentDocument.parentWindow;

 iframe.onload = function(){
 alert(iframeWindow.getElementById('notice'));
 };

Can anyone help me get this working?

with cross domain

thanks for reply

asked Apr 29, 2011 at 17:51
2
  • 2
    Is the irfame src on the same domain? What you posted should work, what happens? (also you should accept some answers!) Commented Apr 29, 2011 at 18:06
  • Undeleted it, the original question I read as asking how to get an iframe reference Commented Apr 29, 2011 at 18:11

3 Answers 3

3

Assuming its all on the same domain;

var ifr = document.getElementById('the_iframes_id');
//or window.frames[x].
var doc = ifr.contentDocument || ifr.contentWindow.document;
alert(doc.getElementById('notice'));

If its not on the same domain, you cannot access it for security reasons.

answered Apr 29, 2011 at 18:04
Sign up to request clarification or add additional context in comments.

2 Comments

i wont to do with cross domain this can not help me
@jayesh: nothing can help you, read up on the same origin policy.
1

You are out of luck if you want to do this across domains. Javascript's security model will not allow it.

I don't know what you are trying to accomplish, but you might be able to get what you need with server-side scripting. A Perl/PHP/Python/Ruby/whatever script generally has the possibility of retrieving any web page you'd need. It could then parse out the bit you need and return this to your Javascript via AJAX calls.

Of course this is more complicated than just using JS, and assumes that the iframe's content is not dynamic, based on cookies or other session things at this moment.

answered Apr 29, 2011 at 18:24

Comments

0
document.getElementById('iframeResult').contentWindow.document.getElementById('buttonId')

where iframeResult is Id of iframe and buttonId is Id of element

answered Jan 3, 2014 at 19:51

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.