Capture Jquery Click on Iframe

So, I have an iframe on my page, for example

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

I was wondering how I can capture the click event on this. Iframe does not have an identifier or class installed on them.

+4
source share
2 answers

You cannot interact with iframes that are not in the same domain. This always prevents browser policy.

See Detecting Click in Iframe Using JavaScript

+1
source

I was faced with a situation where I had to track clicks on a button on social networks pulled through an iframe. When you click the button, a new window opens. Here is 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(); 
+7
source

All Articles