如何在我的 javascript 代码中获取此框架的名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4834686/
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
how to get this frame's name in my javascript code
提问by zjm1126
this is my html code :
这是我的 html 代码:
<frameset rows="18,*" frameborder=0 framespacing=0>
<frame src="/zh-cn/MapS/MainTop/" noresize scrolling=no>
<frameset cols="0,*,210" name="menu">
<frame src="/zh-cn/MapS/MainLeft/" scrolling=no noresize>
<frame src="/zh-cn/MapS/Watch/" name="desk">
<frame src="/zh-cn/MapS/RightMenu/" scrolling=no noresize>
</frameset>
</frameset>
and this is the javascript code in on frame :
这是框架中的 javascript 代码:
parent.parent.frames[2].frames[0]
i want to know which name is this frame ,
我想知道这个框架叫什么名字
i do this :
我这样做:
console.log(parent.parent.frames[2].frames[0].nodename)
it show:
它显示:
undefined
so what can i do ,
那我能怎么办
thanks
谢谢
采纳答案by John K.
This should give you the src value.
这应该给你 src 值。
console.log(parent.parent.frames[2].frames[0].location.href)
回答by Mariusz
For example, this should work
例如,这应该工作
window.frameElement.name
Also, if all you have is a reference to an element inside a frame and want to know the frame name this element belongs to:
此外,如果您所拥有的只是对框架内元素的引用,并且想知道该元素所属的框架名称:
document.getElementsByTagName("body")[0].ownerDocument.defaultView.window.frameElement.name
回答by Elmue
Why are all the answers here so complicated ?
为什么这里的所有答案都如此复杂?
The name of the current document (== frame name) can be obtained via:
当前文档的名称(== 框架名称)可以通过以下方式获得:
var sName = window.name;
If the current document is the root document or if the parent document has not assigned a name to the child frame, sName will be an empty string.
如果当前文档是根文档或者父文档没有为子框架指定名称,则 sName 将为空字符串。
回答by Romulo
Greetings,
你好,
You can create a function to retrieve the frame element name from within the frame content like that:
您可以创建一个函数来从框架内容中检索框架元素名称,如下所示:
function getFrameName(frame) {
var frames = parent.frames,
l = frames.length,
name = null;
for (var x=0; x<l; x++) {
if (frames[x] === frame) {
name = frames[x].name;
}
}
return name;
}
And then call it inside the document:
然后在文档中调用它:
window.onload = function() {
var body = document.getElementsByTagName("body")[0],
name = getFrameName(self);
if (name != null) {
var text = document.createTextNode("this frame name is: " + name);
body.appendChild(text);
}
}
Hope that helps.
希望有帮助。
回答by Emmanuel Mahuni
Why all that trouble? Just
为什么这么麻烦?只是
frames.name
frames.name
gives you the current frame name.
为您提供当前帧名称。
回答by DoctorLouie
Change your HTML to this:
将您的 HTML 更改为:
<html>
<frameset rows="18,*">
<frame name="MainTop" src="/zh-cn/MapS/MainTop/" scrolling="auto" frameborder="0">
<frameset cols="0,*,210">
<frame name="MainLeft" src="/zh-cn/MapS/MainLeft/" scrolling="auto" frameborder="0">
<frame name="Watch" src="/zh-cn/MapS/Watch/" scrolling="auto" frameborder="0">
<frame name="RightMenu" src="/zh-cn/MapS/RightMenu/" scrolling="auto" frameborder="0">
</frameset>
</frameset>
</html>
Test Script:
测试脚本:
console.log(parent.frames[2].name);
回答by snir
Current page:
当前页面:
location.pathname
location.pathname
Current page's address:
当前页面地址:
location.href
location.href
First frame inside the page:
页面内的第一帧:
parent.frames[0].name
parent.frames[0].name
note that:
注意:
window.document.location