简体   繁体   English

从使用jquery创建的iframe调用父函数

[英]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. 我不想编辑b.php。 Is there any way I can call the parent 'callme' function using just javascript in the parent file? 有没有什么办法可以在父文件中使用javascript调用父'callme'函数?

Not sure about the part "I don't want to edit b.php ". 不确定“我不想编辑b.php ”部分。 Your question is asking about how to call the parent function from an iframe and b.php is in the iframe. 您的问题是询问如何从iframe调用父函数,而b.php在iframe中。 How can you make it call the parent callme() without modifying b.php ? 如何在不修改b.php情况下调用父callme()

From b.php you can call b.php你可以打电话

parent.callme();

Or is your question, how can I call callme() after b.php finishes loading? 或者是你的问题,如何在b.php完成加载后调用callme()

To call a parent Javascript function from an iframe you can use: 要从iframe调用父Javascript函数,您可以使用:

window.parent.callme();

Keep in mind this won't work if the iframe is on a different domain. 请注意,如果iframe位于其他域中,则无法使用此功能。 If it's on a different domain you'll need to set both pages document.domain to the same value like: 如果它位于不同的域上,则需要将两个页面document.domain设置为相同的值,如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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