最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - how to get this frame's name in my javascript code- Stack Overflow

programmeradmin2浏览0评论

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

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

Share Improve this question asked Jan 29, 2011 at 2:09 zjm1126zjm1126 35.7k53 gold badges125 silver badges167 bronze badges
Add a comment  | 

7 Answers 7

Reset to default 6

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)

  1. 暂无评论