0

How to capture click on iframe using javascript.

I'm using this code:

document.getElementsByClassName('pin').contentWindow.document.body.onclick = 
function() {
  alert("iframe clicked");
}

But it is not able to capture any click of the iframe. I can only use javascript. I'm getting this error "SecurityError: Blocked a frame with origin"

Timigen
  • 1,055
  • 1
  • 17
  • 33
Aakash Handa
  • 1,215
  • 11
  • 18
  • 1
    Is the iframe coming from the same host / domain? – Ja͢ck Jun 10 '14 at 14:00
  • See if this helps : http://stackoverflow.com/questions/1609741/how-to-add-click-event-to-a-iframe-with-jquery – Patrick Jun 10 '14 at 14:01
  • 1
    `getElementsByClassName` returns a `NodeList` which doesn't have a `contentWindow` property. Either try `document.getElementsByClassName('pin')[0]` or walk on the list. – haim770 Jun 10 '14 at 14:03

1 Answers1

-1
var i = 0; 
//i represents the exact node since obtaining node by class name gives array of nodes.
var iframe = document.getElementsByClassName('pin')[i].contentWindow;

or

var iframe = document.getElementById("iframe_id").contentWindow;

iframe.document.body.onmousedown = 
function() {
  alert("iframe clicked");
}
Chandra Prakash
  • 781
  • 4
  • 13
  • 23