Javascript: get iframe id within loaded page

元气小坏坏 提交于 2019-12-23 06:48:06

问题


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>

回答1:


Try window.frameElement.id in the iframe.




回答2:


In the iframe this is working for me. But only if the iframe and the parent share the same domain:

this.frameElement.attributes.id



回答3:


You have to insert the name of the iframe:

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


来源:https://stackoverflow.com/questions/5864505/javascript-get-iframe-id-within-loaded-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!