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

JavascriptjQuery: calling function in another frame - Stack Overflow

programmeradmin2浏览0评论

I'm new to the DOM and JavaScript and hitting on some problems when trying to call a function defined in a frame from the context of the top-level frame or Firebug.

Given the below frameset:

<html>
    <body>
    <frameset cols="*" rows="81,*">
        <frame id="topFrame" tabindex="1" name="topFrame" noresize="noresize" scrolling="No" src="hometop.aspx"/> 
        <frameset border="0" cols="214,*" frameborder="no" framespacing="0">
            <frameset border="0" cols="*" frameborder="no" framespacing="0" rows="70,*">
                <frame tabindex="-1" id="chatFrame" name="chatFrame" scrolling="No" noresize="noresize" src=""/>
                <frame tabindex="-1" id="leftFrame" name="leftFrame" noresize="noresize" src="leftFrame.aspx"/>
            </frameset> 
            <frame tabindex="-1" id="mainFrame" name="mainFrame" src=""/>
        </frameset> 
        <noframes>Your browser does not support frameset.</noframes> 
    </frameset>
    </body>
</html>

I'm trying to write a javascript hook that will call a javascript function defined in #leftFrame when the above document is first opened. I'm performing this in a Firebug session with jQuery loaded.

jQuery("#leftFrame") returns a frame DOM element. Now I'd like to execute my function (openLink, defined in plain old script tag in leftFrame.aspx) in the context of the frame. My understanding is that the function will be a DOM node under the leftFrame's document element. However I can't get hold of the frame's document.

I've tried:

 jQuery("#leftFrame").document
 jQuery("#leftFrame").contentDocument
 jQuery("#leftFrame").find("html")

Also when inspecting the DOM tree in Firebug I can't see the openLink function under any DOM nodes as I'd expect.

Can anyone help me out?

I'm new to the DOM and JavaScript and hitting on some problems when trying to call a function defined in a frame from the context of the top-level frame or Firebug.

Given the below frameset:

<html>
    <body>
    <frameset cols="*" rows="81,*">
        <frame id="topFrame" tabindex="1" name="topFrame" noresize="noresize" scrolling="No" src="hometop.aspx"/> 
        <frameset border="0" cols="214,*" frameborder="no" framespacing="0">
            <frameset border="0" cols="*" frameborder="no" framespacing="0" rows="70,*">
                <frame tabindex="-1" id="chatFrame" name="chatFrame" scrolling="No" noresize="noresize" src=""/>
                <frame tabindex="-1" id="leftFrame" name="leftFrame" noresize="noresize" src="leftFrame.aspx"/>
            </frameset> 
            <frame tabindex="-1" id="mainFrame" name="mainFrame" src=""/>
        </frameset> 
        <noframes>Your browser does not support frameset.</noframes> 
    </frameset>
    </body>
</html>

I'm trying to write a javascript hook that will call a javascript function defined in #leftFrame when the above document is first opened. I'm performing this in a Firebug session with jQuery loaded.

jQuery("#leftFrame") returns a frame DOM element. Now I'd like to execute my function (openLink, defined in plain old script tag in leftFrame.aspx) in the context of the frame. My understanding is that the function will be a DOM node under the leftFrame's document element. However I can't get hold of the frame's document.

I've tried:

 jQuery("#leftFrame").document
 jQuery("#leftFrame").contentDocument
 jQuery("#leftFrame").find("html")

Also when inspecting the DOM tree in Firebug I can't see the openLink function under any DOM nodes as I'd expect.

Can anyone help me out?

Share Improve this question asked Aug 17, 2010 at 9:29 chillitomchillitom 25.7k17 gold badges88 silver badges122 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7
$('#leftFrame')[0].contentWindow.document
$('#leftFrame')[0].contentWindow.functionName()

The above should work. jQuery's contents() only works on the iframe node so you'll have to reference it like that.

发布评论

评论列表(0)

  1. 暂无评论