Javascript 从 iframe 内部访问 iframe 名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2248951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
access iframe name from inside iframe
提问by Ilia Choly
I though this would be simple enough but I can't find the answer. I need to know how I can access an iframe's name from within said iframe. I was trying something like this but it's not working.
我虽然这很简单,但我找不到答案。我需要知道如何从上述 iframe 中访问 iframe 的名称。我正在尝试这样的事情,但它不起作用。
<iframe name="thename">
<script type="text/javascript">
alert(parent.name);
</script>
</iframe>
回答by efirat
There is a combination of answers I prefer:
我更喜欢以下几种答案的组合:
// window.frameElement Gets IFrame element which document inside
window.frameElement.getAttribute("Name");
It works on IE7+, Mozilla & Chrome
它适用于 IE7+、Mozilla 和 Chrome
回答by bobince
You were nearly right. Setting the nameattribute on a frameor iframesets the nameproperty of the frame's global windowobject to that string. (Not parent, which refers to the window of the document that owns the frame.)
你几乎是对的。name在 a 上设置属性frame或iframe将name框架的全局window对象的属性设置为该字符串。(不是parent,它指的是拥有框架的文档的窗口。)
So unless some other script has deliberately changed the name, it's as simple as:
因此,除非某些其他脚本故意更改名称,否则很简单:
1.html:
<iframe name="tim" href="2.html"></iframe>
2.html:
<script type="text/javascript">
alert(window.name); // tim
</script>
回答by PayteR
in some cases window.frameElementreturns nullin iframe, so i figured workaround for it.
在某些情况下,在 iframe 中window.frameElement返回null,所以我想了解决方法。
1. you need to set hash in src url of iframe
1.你需要在iframe的src url中设置hash
<iframe name="tim" href="2.html#your-hash"></iframe>
2. in iframe you can get this hash with
2.在iframe中你可以得到这个哈希
<script type="text/javascript">
var hash = window.location.hash;
</script>
回答by bdl
Well, an IFRAME element shouldn't contain anything, it's targeting another document. So using a SCRIPT tag inside an IFRAME doesn't make alot of sense. Instead, use the SCRIPT inside the called document, e.g.
嗯,一个 IFRAME 元素不应该包含任何东西,它的目标是另一个文档。所以在 IFRAME 中使用 SCRIPT 标签没有多大意义。相反,在被调用的文档中使用 SCRIPT,例如
iframe_caller.html:
iframe_caller.html:
<html>
<body>
<iframe id="theIframe" name="theIframe" src="iframe_doc.html"></iframe>
</body>
</html>
iframe_doc.html:
iframe_doc.html:
<html>
<body>
<script type="text/javascript">
var iframes= parent.document.getElementsByTagName("iframe");
document.write(iframes[0].getAttribute("id"));
</script>
</body>
</html>
Note I'm using parent.document.function()there.
注意我在parent.document.function()那里使用。
回答by Todd Moses
Something like this should work:
这样的事情应该工作:
parent.document.getElementById('idhere').name;
You have to use the parent and then get the element either byId, Name, etc... then access the name property.
您必须使用父级,然后通过 Id、Name 等获取元素...然后访问 name 属性。
So your code should be like:
所以你的代码应该是这样的:
<iframe name="thename">
<script type="text/javascript">
var iframeName = parent.document.getElementById('idhere').name;
</script>
</iframe>

