How to use jquery from parent inside an iframe?

后端 未结 3 1266
萌比男神i
萌比男神i 2020-12-08 22:25

I have a file, let us call it parent-win.html, served by domain example.com. This file has jquery included in it. An iframe, lets call it iframe-win.html, is embedded in thi

相关标签:
3条回答
  • 2020-12-08 23:03

    Here is a better solution based on the Moin's answer:

    if (typeof(jQuery) == "undefined") {
        window.jQuery = function (selector) { return parent.jQuery(selector, document); };
        jQuery = parent.$.extend(jQuery, parent.$);
        window.$ = jQuery;
    }
    

    So we can use $'s functions like $.get, $.extends, etc.

    0 讨论(0)
  • 2020-12-08 23:11

    try this in the iframe:

    if (typeof(jQuery) == "undefined") {
        var iframeBody = document.getElementsByTagName("body")[0];
        var jQuery = function (selector) { return parent.jQuery(selector, iframeBody); };
        var $ = jQuery;
    }
    
    0 讨论(0)
  • 2020-12-08 23:19

    You need to load jQuery inside the iframe.

    EDIT: Okay, if the frame is not on the same domain, then you do not have to reload jQuery. See Access jQuery library from iframe

    0 讨论(0)
提交回复
热议问题