Jquery run function from child window in parent window
I have a popup window and I want to run a function from in the parent window.
In the child I have:
$(document).ready(function(){
parent.$.fn.guestFromPop('Action');
});
In the parent I have:
$(document).ready(function(){
function guestFromPop(data) {
alert(data);
}
});
The problem is when parent.$.fn.guestFromPop('Action');
I never see the alert, nothing happens. Am开发者_开发知识库 I doing something wrong?
This doesn't declare the function $.fn.guestFromPop
:
$(document).ready(function(){
function guestFromPop(data) {
alert(data);
}
});
...all it does is declare guestFromPop()
, a function only available inside that document.ready
handler. You would need to declare the function you're after instead:
$.fn.guestFromPop = function(data) {
alert(data);
};
Though, this isn't really correct either, since it wont be called on a jQuery element, you may want something as simple as :
function guestFromPop(data) {
alert(data);
}
called by:
parent.guestFromPop('Action');
(function(){
$.fn.TestFunction=function(SampleData)
{
alert(SampleData);
};
});
//callable in other parent page or iframe //
(function(){
$.fn.TestFunction("sdfsdfsfsfsd");
});
I had the same problem. Figured it out. The problem with your code is that you're not referring to the right window to call the function from. To get from a console to it's parent you'll need 'opener', not 'parent'.
So instead of parent.guestFromPop('Action'), you need to call this:
$(self.window.opener.document).guestFromPop('Action');
That should refer to the correct function. It worked for me! ;-)
guestFromPop = function(data) {
alert (data)
}
call from iframe:
parent.guestFromPop(data);
精彩评论