Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
170
rated 0 times [  176] [ 6]  / answers: 1 / hits: 19339  / 12 Years ago, fri, june 8, 2012, 12:00:00

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 this page. iframe-win.html has a form element with id form-elem and value Hello World!. Now I do following inside iframe.



var jQuery = parent.jQuery;
console.log(jQuery('#form-elem').val());


According to my limited knowledge of JS I should see Hello World! on console but instead I see undefined. Now, my question is do I need to load jquery again in an iframe or can I utilise jquery object already loaded in parent window?



Do note that this is not as usual access iframe/parent content from parent/iframe.


More From » jquery

 Answers
10

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;
}

[#85053] Thursday, June 7, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marcelofrankiea

Total Points: 200
Total Questions: 96
Total Answers: 101

Location: Tonga
Member since Tue, Nov 30, 2021
3 Years ago
;