Reputation: 11
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
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
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