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.