Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[jQuery]Find click inside an iFrame

Tags:

jquery

iframe

I have a page with an iframe.

<div id="my_content">
  <iframe id="my_iframe" src="http://mysite.com/iframe.html">
</div>

I need to intercept the click inside the iframe. I've tried with this code but it doesn't work:

<script type="text/javascript">


jQuery(document).ready(function(){
 jQuery('#my_iframe').click(function(){ 
  alert('click inside iframe');
 });
});
like image 290
Pennywise83 Avatar asked Aug 02 '26 06:08

Pennywise83


1 Answers

Assuming that the iframe source is on the same domain as the outer page, you can use the contents method:

The .contents() method can also be used to get the content document of an iframe, if the iframe is on the same domain as the main page.

jQuery(document).ready(function(){
  jQuery('#my_iframe').contents().click(function(){ 
    alert('click inside iframe');
  });
});
like image 131
Rich O'Kelly Avatar answered Aug 04 '26 14:08

Rich O'Kelly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!