admin管理员组

文章数量:1415058

I'm having issues accessing a frame that is part of a page loaded into a iframe. The frame is nested in multiple framesets.

My html looks like this:

<iframe name="sourceframe" src="siteindex.html">
    #document <-- this appears in the dom tree. Not sure if it's relevant.
        <html>
            <head></head>
            <frameset>
                ...other frames
                <frameset>
                    <frame name="targetframe" src="sitesubpage.html">
                        ...I want to access this frame and replace the contents with a different html page...
                    </frame>
                </frameset>
            </frameset>
        </html>
</iframe>

I have read similar questions none of which have a solution that I can get to work.

Any suggestions would be greatly appreciated.

I'm having issues accessing a frame that is part of a page loaded into a iframe. The frame is nested in multiple framesets.

My html looks like this:

<iframe name="sourceframe" src="siteindex.html">
    #document <-- this appears in the dom tree. Not sure if it's relevant.
        <html>
            <head></head>
            <frameset>
                ...other frames
                <frameset>
                    <frame name="targetframe" src="sitesubpage.html">
                        ...I want to access this frame and replace the contents with a different html page...
                    </frame>
                </frameset>
            </frameset>
        </html>
</iframe>

I have read similar questions none of which have a solution that I can get to work.

Any suggestions would be greatly appreciated.

Share Improve this question edited Jul 10, 2013 at 23:58 ThunderHorse asked Jul 10, 2013 at 22:18 ThunderHorseThunderHorse 3751 gold badge4 silver badges18 bronze badges 5
  • stackoverflow./questions/1644676/… I can't get method used in this answer to work when navigating the framesets. – ThunderHorse Commented Jul 10, 2013 at 22:26
  • document.frames['sourceframe'].document.frames['targetframe'] supposed to contain a reference to the window of the #targetframe, from the page where #sourceframe is placed. If this is not what you need , please elaborate your question, from where to which element / object you need the reference. – Teemu Commented Jul 10, 2013 at 22:27
  • @Teemu that is what I need. I seem to have something setup wrong. Checking document.frames.length from the page where my source iframe is gives 'Cannot read property 'length' of undefined' Should it not be 1? – ThunderHorse Commented Jul 10, 2013 at 22:42
  • Odd... Try window instead document. Which browser are you using? Also are you sure the iframe exists at the time you're reading the length of the frames? This is better to do within window.onload handler function. – Teemu Commented Jul 10, 2013 at 22:43
  • @Teemu The iframe is created window.onload (in the head). Using chrome Version 27.0.1453.116. window.frames['sourceFrame'].frames.length gives a length of 0. – ThunderHorse Commented Jul 10, 2013 at 23:00
Add a ment  | 

2 Answers 2

Reset to default 9
window.frames['sourceframe'].frames['targetframe'].location.href = 'new_html_page';

FF and Chrome have frames collection only in window, IE and Opera have it also in the document. The above code works (tested), when you get your timing corrected.

Try to use some timeout before changing the the href, or trigger the change from the #targetframe. Also you can attach an onload event listener to the iframe, and trigger the change in it. One more, you can place the script creating the iframe just before closing body tag, then you can use window.onload to change the page in the depths...

You could give your frame element with name='targetframe' an id='targetframe' and do something like:

function E(e){
  return document.getElementById(e);
}
function getChildFrame(id){
  var t = E(id);
  var f = t.contentWindow || t.contentDocument;
  if(f.document)f = f.document;
  return f;
}
var tf = getChildFrame('targetframe');
tf.style.backgroundColor = '#007';

See

http://www.w3schools./jsref/prop_frame_contentwindow.asp

and

http://www.w3schools./jsref/prop_win_frames.asp

for more details. Of course, the Same Origin Policy applies. In other words you can't access someone else's frame, without permission.

By the way, you can access the frames array with a number as well.

本文标签: htmlJavascriptaccessing nested frames in framesets from a iFrameStack Overflow