tweakmy
tweakmy

Reputation: 11

javascript: getting the id of the parent from a document

I am having some trouble to get the id of the parent of a document which is "iframe1" here. As you can see the javascript should be residing in the "" section. Do mind giving me some ideas?

Yes, I know. I can insert the javascript on the top, but believe me i cant for some purpose because I am intergrating many other codes into it.

 <html>
     <iframe id="iframe1">
     <html>
         <head>
                <javascript>
         </head>
         <body>
         </body>
      </html>
      </iframe> 
</html>

Upvotes: 1

Views: 112

Answers (2)

Redlab
Redlab

Reputation: 3118

add a name attribute to the iframe. Then you should be able to call window.name in javascript.

e.g.

<html>
    <head></head>
    <body>
        <iframe id="iframe1" name="iframe1" src="x.html">
        </iframe> 
   </body>
</html>

x.html

<html>         
     <head></head>
        <body>
          <script type="text/javascript">alert(window.name);</script>
        </body>
</html>

Upvotes: 0

Andy E
Andy E

Reputation: 344605

Assuming the frame is on the same domain as the parent, you can use .frameElement to get a reference to the containing iframe. If it's not on the same domain, security restrictions will prevent you from accessing the parent document.

var frameId = window.frameElement.id;

Upvotes: 1

Related Questions