4

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

7 Answers 7

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
Sign up to request clarification or add additional context in comments.

Comments

4

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.

3 Comments

window.frameElement won't work with CORS. but window.name inside will be set to the same value as on the iframe
The question was how to get "this frame's name". So CORS is not a topic here. window.name will work always inside the same frame.
@Elmue ... not necessarily assuming one has control over some/all CORS domains/script ends
1

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.

Comments

0

This should give you the src value.

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

1 Comment

Again a wrong answer is marked as the correct answer. This gives the Url, not the name!
0

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

1 Comment

By following the DOM you'll see that MainTop is 0, MainLeft is 1, Watch is 2 and RightMenu is 3. Based on the actual modeling of the document.
0

Why all that trouble? Just

frames.name

gives you the current frame name.

Comments

0

Current page:

location.pathname

Current page's address:

location.href

First frame inside the page:

parent.frames[0].name

note that:

window.document.location

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.