Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access iframe elements with Javascript?

I have a webpage where there is a textarea within a iframe. I need to read the value of this textarea from its child page JavaScript. Presently by using window.parent.getelementbyID().value in the JavaScript, I am able to fetch values of all controls in the parent page except the textarea within the iframe.

The frame id and frame name in my parent page changes in runtime, hence we cannot use the frame id/frame name for reference.

like image 866
archana roy Avatar asked Sep 21 '09 04:09

archana roy


People also ask

Can JavaScript access iframe content?

You could use . contents() method of jQuery. The . contents() method can also be used to get the content document of an iframe, if the iframe is on the same domain as the main page.

How do I check if an element is an iframe?

In short, to check if a page is in an iframe, you need to compare the object's location with the window object's parent location. If they are equal, then the page is not in an iframe; otherwise, a page is in an iframe.


1 Answers

If you have the HTML

<form name="formname" .... id="form-first">     <iframe id="one" src="iframe2.html">     </iframe> </form> 

and JavaScript

function iframeRef( frameRef ) {     return frameRef.contentWindow         ? frameRef.contentWindow.document         : frameRef.contentDocument }  var inside = iframeRef( document.getElementById('one') ) 

inside is now a reference to the document, so you can do getElementsByTagName('textarea') and whatever you like, depending on what's inside the iframe src.

like image 104
meder omuraliev Avatar answered Sep 19 '22 08:09

meder omuraliev