I want to detect click on an iframe. The suggested way to do this is to catch blur event on parent window. But when user clicks on svg within an iframe, it doesn't take the focus and the root window does not lose one. Is there a way to force iframe to take focus when user clicks on any content within it? Or detect the click in any other way.
Here are two examples. The only difference is iframe src url:
Working example: http://plnkr.co/edit/Av6A2dzlfl2K9xYRl1C2
Non-working example: http://plnkr.co/edit/BVm0jL69XucbTNQ6ilKC
JS:
$(document).ready(function() {
var overiFrame = false;
$('iframe').hover(function() {
overiFrame = true;
$(window).focus();
}, function() {
overiFrame = false;
});
$(window).blur(function() {
if (overiFrame) {
alert("it works!");
}
});
});
HTML:
<body>
<h1> Detect click on iframe </h1>
<iframe src="http://phet.colorado.edu/sims/html/forces-and-motion-basics/latest/forces-and-motion-basics_en.html"></iframe>
</body>