最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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/

发布评论

评论列表(0)

  1. 暂无评论