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
1 Answer
Reset to default 4Using 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/