Access jQuery library from iframe

前端 未结 2 1989
眼角桃花
眼角桃花 2020-12-17 01:48

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

相关标签:
2条回答
  • 2020-12-17 02:12

    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;
    
    0 讨论(0)
  • 2020-12-17 02:25

    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 讨论(0)
提交回复
热议问题