0

How can I check with javascript when mouse click on iframe, because iframe don't support "on" events?

That is my iframe:

And I use this jquery code:

$('#ifr31').bind('click', function(event) { alert(1) });

But when I click on iframe don't alert nothing?

Defense
  • 259
  • 4
  • 21

3 Answers3

1

Taken from this answer:

https://stackoverflow.com/a/1609808/561545

There's no 'onclick' event for an iframe, but you can try to catch the click even of the document in the iframe:

document.getElementById("iframe_id").contentWindow.document.body.onclick = function() { alert("iframe clicked"); }

EDIT Though this doesn't solve your cross site problem, FYI jQuery has been updated to play well with iFrames:

$('#iframe_id').bind('click', function(event) { });

Community
  • 1
  • 1
Jeff
  • 12,085
  • 12
  • 82
  • 152
0

if same origin you can use $('#ifame').bind('click', function(event) { });

Arun Killu
  • 13,581
  • 5
  • 34
  • 61
0

Only works on iframes on the same domain:

$('body', $('select-your-iframe-here').contents()).click(function(event) {
  console.log('Clicked! ' + event.pageX + ' - ' + event.pageY);
});
rahul
  • 7,573
  • 7
  • 39
  • 53