开发者

how to get this frame's name in my javascript code

this is my html code :

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

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


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


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.


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.


This should give you the src value.

console.log(parent.parent.frames[2].frames[0].location.href)


Change your HTML to this:


<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);


Why all that trouble? Just

frames.name

gives you the current frame name.


Current page:

location.pathname

Current page's address:

location.href

First frame inside the page:

parent.frames[0].name

note that:

window.document.location
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜