开发者

Access jQuery library from iframe

I have a legacy application that uses iframes. It has an iframe in the parent page, that is dynamically replaced with other pages.

JQuery is loaded in the parent. Is there some type of plugin that will allow me to access the jquery core that is loaded in the parent from the iframe pages without including jquery (language="JavaScript" src="../javascript/jquery.js") in the multiple child (iframe) pages?

For example, the iframe is static

<iframe name="mainWindow" src="includes/View.asp frameborder=开发者_如何学编程"0" />

I know there are better ways to do this, but I am stuck with this architecture at the moment. Any suggestions?


You could try running this from inside your iframe:

var $ = jQuery = window.parent.$;

Assuming the parent and iframe are on the same domain.

Just tested it, example: http://jsfiddle.net/qA7yE/

(That example uses different code - the child iframe calls the parent's foo() function, but the same principle applies)

Also, just to be on the safe side you may want to do:

var $ = window.parent.$, jQuery = window.parent.jQuery;


Not valid, the object refers to the parent, not the iframe. Try a simple $('body').css('background','red') inside the iframe; it applies to the parent not the iframe.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜