Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: get iframe id within loaded page

Tags:

I've got a page that has an iframe. Every time the page loads, iframe gets unique id and name assigned to it. I can get the iframe name within loaded iframe like so:

alert(parent.window.frames[window.name].name); 

But when i try to get the id value:

alert(parent.window.frames[window.name].id); 

I get undefined?

Is it possible to get the id attribute of the iframe within loaded page?


http://jsfiddle.net/cqFtB/


<iframe id="lyygi8stwZSANUEh" src="http://example.com" name="zma82vRVe18xbAqW" title="Awesome Iframe"> 

example.com:

<!DOCTYPE html> <html lang="en"> <head>     <meta charset="utf-8">     <title>Awesome Iframe</title> </head> <body>     <script type="text/javascript">          alert(parent.window.frames[window.name].name);     </script> </body> </html> 
like image 658
Pav Avatar asked May 03 '11 02:05

Pav


People also ask

How do I find the iframe id?

Try window.frameElement.id in the iframe.

Can an iframe have an ID?

The id attribute assigns an identifier to the <iframe> element. The id allows JavaScript to easily access the <iframe> element. It is also used to point to a specific id selector in a style sheet.

How can I access iframe element with JavaScript?

Getting the element in Iframeconst iframe = document. getElementById("myIframe"); Now, it has and contentWindow property which returns the document object by using that we can access the elements from an Iframe. const iWindow = iframe.


1 Answers

Try window.frameElement.id in the iframe.

like image 51
Boris Zbarsky Avatar answered Sep 16 '22 15:09

Boris Zbarsky