johnlemon
johnlemon

Reputation: 21449

Call parent function from iframe created with jquery

Consider this code:

function callme() {
    alert('call');
}

$(function(){ 
    var iframe = $('<iframe />').attr('src', 'b.php').appendTo('body'); 
    //call 'callme' function from parent
});

I don't want to edit b.php. Is there any way I can call the parent 'callme' function using just javascript in the parent file?

Upvotes: 0

Views: 4065

Answers (2)

MrCode
MrCode

Reputation: 64526

To call a parent Javascript function from an iframe you can use:

window.parent.callme();

Keep in mind this won't work if the iframe is on a different domain. If it's on a different domain you'll need to set both pages document.domain to the same value like:

// in both the parent and iframe
document.domain = 'site.com';

Upvotes: 0

Ruan Mendes
Ruan Mendes

Reputation: 92274

Not sure about the part "I don't want to edit b.php". Your question is asking about how to call the parent function from an iframe and b.php is in the iframe. How can you make it call the parent callme() without modifying b.php?

From b.php you can call

parent.callme();

Or is your question, how can I call callme() after b.php finishes loading?

Upvotes: 2

Related Questions