javascript - Handling iFrame event in the parent web page - Stack Overflow

I have a question about municating between a iFrame and parent or containing web page. Can I trigger an

I have a question about municating between a iFrame and parent or containing web page.

Can I trigger an event in iFrame and handle it in the parent containing web page containing it? The iFrame and the parent page belongs to same sub-domain.

I have a question about municating between a iFrame and parent or containing web page.

Can I trigger an event in iFrame and handle it in the parent containing web page containing it? The iFrame and the parent page belongs to same sub-domain.

Share Improve this question edited Mar 26, 2020 at 16:30 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 7, 2013 at 0:41 TarunTarun 8481 gold badge14 silver badges32 bronze badges 2
  • 2 If they are from the same subdomain, yes. The parent page and iframe should be able to interact. – user1600124 Commented Sep 7, 2013 at 0:44
  • 2 This may help you. – The Alpha Commented Sep 7, 2013 at 0:45
Add a ment  | 

1 Answer 1

Reset to default 4

Using the link Sheikh Heera provided in the ments, yes, you can trigger events in an iframe and handle it in the parent.

var doc = iframe.contentDocument || iframe.contentWindow.document;
doc.getElementById("myDiv").addEventListener('click',function() {
    //doStuffHere
});

Here's a fiddle for it: http://jsfiddle/9sfm9/


OR just call parent.doFunction() in the iframe's JavaScript, assuming, doFunction() is a function in the parent page. Example:

parent script:

var doFunction = function() {
    alert("I was triggered from an iframe");
}

iframe script:

parent.doFunction();

And here's a fiddle for that: http://jsfiddle/9sfm9/2/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742288112a4415689.html

相关推荐

  • javascript - Handling iFrame event in the parent web page - Stack Overflow

    I have a question about municating between a iFrame and parent or containing web page. Can I trigger an

    19小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信