views:

161

answers:

1

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?

+3  A: 
$('#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.

meder
`contentWindow` is an IE extension not standardised until HTML5 and with support issues in older versions of the other browsers. Typically you should branch code on the presence of the long-standard `contentDocument` property first (and use `defaultView` on it to get the associated `window`), falling back to `contentWindow` for IE6-7.
bobince