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 thewindow
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
insteaddocument
. Which browser are you using? Also are you sure theiframe
exists at the time you're reading the length of theframes
? This is better to do withinwindow.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
2 Answers
Reset to default 9window.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
版权声明:本文标题:html - Javascript, accessing nested frames in framesets from a iFrame - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740858201a2297243.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论