问题
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?
回答1:
$('#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.
来源:https://stackoverflow.com/questions/3501080/javascript-jquery-calling-function-in-another-frame