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

html - Call javascript function from frame in other frameset - Stack Overflow

programmeradmin0浏览0评论

My site is setup like this:

<frameset rows="80,*">
    <frame name="top" id="top" src="header.html">
    <frameset id="innerframe" cols="300,*">
        <frame name="nav" src="nav.html">
    </frameset>
</frameset>


In header.html I have:

function fAlert() {
    alert('test');
}

How can I call fAlert() in nav.html?

I tried

var fframe = parent.document.getElementById('top');
fframe.fAlert();

and also

parent.frames.top.fAlert();

but it didnt work (fAlert is undefined).

Any ideas how I can accomplish this?

My site is setup like this:

<frameset rows="80,*">
    <frame name="top" id="top" src="header.html">
    <frameset id="innerframe" cols="300,*">
        <frame name="nav" src="nav.html">
    </frameset>
</frameset>


In header.html I have:

function fAlert() {
    alert('test');
}

How can I call fAlert() in nav.html?

I tried

var fframe = parent.document.getElementById('top');
fframe.fAlert();

and also

parent.frames.top.fAlert();

but it didnt work (fAlert is undefined).

Any ideas how I can accomplish this?

Share Improve this question asked Nov 20, 2013 at 14:26 sephseph 6942 gold badges8 silver badges23 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 8

First off, don't use framesets and frames. Use iframes--frames are deprecated.

Secondly, provide an id for the iframe (or frame, if you must) in order to direct the function call correctly. (You've already pretty much done this, but I'm being methodical.) I wouldn't name it 'top' because 'top' already has a meaning in terms of windows and frames.

From inside the nav frame, parent.insertYourFrameIdHere.fAlert() should work correctly. This assumes two things: 1) The page and the frame contents come from the same domain, and 2) header.html loaded correctly and there were no script errors in it. Script errors or other issues could keep the function from ever being created.

For the html in your question following should work.

window.parent.parent.frames[0].fAlert();
发布评论

评论列表(0)

  1. 暂无评论