This question has been asked a lot, and the answers are usually similar.
In basic terms, I need a function to perform when the contents of an iframe are clicked.
For the purposes of this demonstration, I have http://mydomain.com/iframe.html which contains an iframe with an ID of "iframeID" and a source of http://mydomain.com.
This code works:
jQuery(document).ready(function(){
$('#iframeID').contents().click(function(event) {
console.log('Clicked! ' + event.pageX + ' - ' + event.pageY);
});
});
But, this code only works in Internet Explorer. I need a solution that will work in all browsers.
The good news is, this is all on the same domain so if I need to put additional code in either the iframe source or my iframe.html then that's fine. I just need it to work cross-browser.
One other point to note, is I have a desire for this to work across subdomains.
I know it won't work with different domains, but as I understand it, subdomains should be fine? Any extra steps required for that to work?
Any help gratefully received!
Here is an example I've put together on jsFiddle that demonstrates one way to use XDM: This is the demo and it includes this fiddle as a child iframe
HTML (parent):
JavaScript (parent):
HTML (child):
JavaScript (child):
Further reading:
If you just want to make links in the iframe open in the top window instead of inside of the iframe check out How to force link from iframe to be opened in the parent window
Just put this in the head of the iframe HTML: