0

I am trying to make a script which detects clicks on google ads. All events works like onmouseover, out etc except click, focus, mousedown. When i click on google ads it open its ads link but dont run my jquery script on click event. I have tried preventdefault and other functions like that too. I want to detect click on google ads and parse the iframe width and height to php file when clicked.

$(window).load(function(){
    $('iframe').bind('mouseover', function(){
        var iframeID = $(this).attr('id');
        $(this).contents().find('html').unbind();
        $(this).contents().find('html').bind('click', function(){
            alert(iframeID);
        });
    });
});

This is the code run on mouseover but not on click event.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Burhan
  • 263
  • 4
  • 9

2 Answers2

1

You need to bind the click events on the contents of the iframe untill the contents are loaded completely inside the iframe. Try wrapping the code inside the load event.

  $('iframe').load(function() {
       var iframeID = $(this).attr('id');
       $(this).contents().find('html').unbind();
       $(this).contents().find('html').bind('click', function(){
           alert(iframeID);
       });
  });
Ashis Kumar
  • 6,494
  • 2
  • 21
  • 36
0

Capturing the click event of an iFrame is not possible when the content is from another domain.

What you can do is detect the mouse-enter and leave on iframe.

Here is example

<div class="item">
<iframe src="http://www.google.com" width="612" height="344" frameborder="0"></iframe>
</div>

<script type="text/javascript">
$("div.item iframe")
.mouseover(function(){
    alert("mouse over");
})
.mouseout(function(){
    alert("mouse out");
});
</script>
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
  • i am making a google analytic type script and for your information statcounter detect clicks on google ads and give info in its exit link activity section. I want to do same as statcounter do – Burhan Aug 05 '13 at 03:25