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"

3
  • 1
    Is the iframe coming from the same host / domain? Commented Jun 10, 2014 at 14:00
  • See if this helps : stackoverflow.com/questions/1609741/… Commented Jun 10, 2014 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. Commented Jun 10, 2014 at 14:03

1 Answer 1

-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");
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.