4

So I have an Iframe on my page like

<iframe width="640" height="360" frameborder="0" allowfullscreen="" src="http://www.youtube.com/xxxxxx">

I was wondering how I could capture the click event on this. The Iframe's do not have an id or class set on them

StevieB
  • 6,263
  • 38
  • 108
  • 193
  • Your answer might be here: http://stackoverflow.com/questions/11223210/capture-mouse-right-click-event-inside-a-iframe-using-jquery –  Mar 21 '13 at 20:16
  • possible duplicate of [How to capture click event with jQuery for iframe?](http://stackoverflow.com/questions/5082623/how-to-capture-click-event-with-jquery-for-iframe) – Anujith Mar 21 '13 at 20:19

2 Answers2

7

I ran into a situation where I had to track clicks on a social media button pulled in through an iframe. A new window would be opened when the button was clicked. Here was my solution:

var iframeClick = function () {
    var isOverIframe = false,
    windowLostBlur = function () {
        if (isOverIframe === true) {
            // DO STUFF
            isOverIframe = false;
        }
    };
    jQuery(window).focus();
    jQuery('#iframe').mouseenter(function(){
        isOverIframe = true;
        console.log(isOverIframe);
    });
    jQuery('#iframe').mouseleave(function(){
        isOverIframe = false;
        console.log(isOverIframe);
    });
    jQuery(window).blur(function () {
        windowLostBlur();
    });
};
iframeClick();
pizzarob
  • 11,711
  • 6
  • 48
  • 69
2

You can not interact with iframes which are not under the same domain. This is always prevented by the browser's policy.

See Detect Click into Iframe using JavaScript

Community
  • 1
  • 1
powtac
  • 40,542
  • 28
  • 115
  • 170