1

Good day all. I've the need to use jQuery of the parent window within an iframe, without adding any script to the page (so adding another jQuery in the iframe page is not possible).

Actually I've worked this solution to reference the parent jQuery into the iframe:

var $ = function (selector) { return parent.jQuery(selector, document.getElementsByTagName("body")[0]); };

By using this, I can normally use code like:

$("p").text("hey jQuery");

But, when I try to use something like:

$(document).ready(function() {

        parent.$.ajax({
            url: '/resources/service_data.json',
            dataType: "json",
            type: 'GET',
            async: true,
            success: function(data) {
                render(data);
            },
            error: function() {
                console.log('Error en la petición');
            }
        });
});

This error is thrown in the console:

Uncaught TypeError: $.ajax is not a function

I've a workaround for this, by using parent.$.ajax() but then I got some more errors, when I try to use more libraries from the iframe, for example this:

$('.carousel-news').owlCarousel({
                items: 1,
                margin: 30,

Of course, owlCarousel is not defined, te question is:

Is there a way to reference jQuery properly into the iframe, so I can use the ajax (for example, or the $.each()) AND is there a way to reference the other libraries within the iframe?

Unfortunately adding them as a script tag in the iframe is not an option (I've been told not to add any tag in the iframe, I can only write one for adding my own code).

Thanks in advance.

EDIT: this is not a duplicate of the question flagged, first because that question didn't solve any problem, second because this question is totally different, I'm already using part of jQuery in my iframe I'm asking how to reference the all library, with all methods, and how to do for other ones.

Matteo Bononi 'peorthyr'
  • 2,170
  • 8
  • 46
  • 95
  • Is there any particular reason why you need the parent jQuery? You could just import jQuery in the frame, and then its guaranteed to work cross-domain as well. – Aeolingamenfel Dec 20 '16 at 15:28
  • I cannot add any script tag to the iframe but the one in which I write my own code. – Matteo Bononi 'peorthyr' Dec 20 '16 at 15:30
  • Alright, next question: why are you using an iFrame in this situation? What is the need? – Aeolingamenfel Dec 20 '16 at 15:32
  • Why don't you concat jQuery with your code and add that as the only script tag to the iframe's source code? – retoheusser Dec 20 '16 at 15:34
  • this is a very very very big project done in Extjs, my job is to do some of the parts of this huge project, and all I got are some Extjs routes, I will have to code some panels in iframes that will be added to the project. unluckily I cannot put my hands in the parent project, the only thing I can ask is to import more libraries (jQuery is one of them, for example). – Matteo Bononi 'peorthyr' Dec 20 '16 at 15:35
  • @retoheusser: I've tryed but every line of code I write is also been reviewed, and adding jquery as you say is something that they don't want (from this the block of adding a script tag) – Matteo Bononi 'peorthyr' Dec 20 '16 at 15:36
  • what I actually know is that any library that I want will be added to the app.json of Ext js and will be compiled with the rest of the code. – Matteo Bononi 'peorthyr' Dec 20 '16 at 15:40
  • Can you pass around other data using `parent` such as a String? I wonder if it's possible to `eval` the jquery source code in the iFrame. – max pleaner Dec 20 '16 at 16:03
  • To recap: You have control of a page that is a child displayed in an iframe that's hosted on a parent page. `parent`.METHOD() works when referencing DOM on parent's page, *but* AJAX get's errors. You need to create some panels...are these panels on the child page? What ***specifically** is your objective? Do you need to send data to parent? Do you need to manipulate the DOM of the parent? Are you allowed to collaborate with the other developers? That seems like the best route, is such interaction discouraged or/and forbidden? That is very counter-productive. – zer00ne Dec 20 '16 at 16:42
  • Possible duplicate of [Access jQuery library from iframe](http://stackoverflow.com/questions/5481613/access-jquery-library-from-iframe) – EhsanT Dec 20 '16 at 18:43
  • It is not a duplicate due to the different context and I'm actually asking how to generalize it – Matteo Bononi 'peorthyr' Dec 20 '16 at 23:17
  • @EhsanT you flagged this question as duplicate... may you please read the answers of the question you pointed and tell me which one should be the answer? – Matteo Bononi 'peorthyr' Dec 21 '16 at 07:46
  • 1
    OK, fair enough, I removed the flag. Also about your problem, I do not think there is a logical way to solve it yourself and as other users mentioned in their comments like @zer00ne, I think it would be better for you and the other part of the project team(who won't let you to add script tag) to collaborate with each other. – EhsanT Dec 22 '16 at 03:29

0 Answers0