0

How can I detect a link click inside a iFrame?

The Frame source changes a lot of times and I need to detect all link click inside the iFrame.

I try that already: Javascript - click link in iframe

JS:

$("#Source a").click();

HTML:

<iframe src="" name="Source" id="Source" scrolling="" frameborder="0" title="Anzeigebereich"></iframe>
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79

3 Answers3

1

you need to use .contents() http://api.jquery.com/contents/

$('#Source').on('load', function(){
    $(this).contents().find('body').on('click', 'a', function(e){
        e.preventDefault(e);//stop normal navigation
        //your code here
    });
});
Clint
  • 973
  • 7
  • 18
0

If you trying to catch an event or do whatever inside the iframe from the oustide document, it isn't possible, the iframe is different context (different document) and this is not alowed. You can check a similar question here

marinoff
  • 171
  • 3
  • 6
0

use post message, for communicaton between frames, also support different domains

post message doc

Gianluca Musa
  • 755
  • 7
  • 22