简体   繁体   中英

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?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM