I have an iframe on a page, coming from a 3rd party (an ad). I'd like to fire a click event when that iframe is clicked in (to record some in-house stats). Something like:
$('#iframe_id').click(function() {
//run function that records clicks
});
..based on HTML of:
<iframe id="iframe_id" src="http://something.com"></iframe>
I can't seem to get any variation of this to work. Thoughts?
You can use this code to bind click an element which is in iframe.
I was trying to find a better answer that was more standalone, so I started to think about how JQuery does events and custom events. Since click (from JQuery) is just any event, I thought that all I had to do was trigger the event given that the iframe's content has been clicked on. Thus, this was my solution
Try using this : iframeTracker jQuery Plugin, like that :
There's no 'onclick' event for an iframe, but you can try to catch the click event of the document in the iframe:
EDIT Though this doesn't solve your cross site problem, FYI jQuery has been updated to play well with iFrames:
Update 1/2015 The link to the iframe explanation has been removed as it's no longer available.
Note The code above will not work if the iframe is from different domain than the host page. You can still try to use hacks mentioned in comments.
This may be interesting for ppl using Primefaces (which uses CLEditor):
I basically just took the answer from Travelling Tech Guy and changed the selection a bit .. ;)
See this: