Jquery - intercept links clicked inside an iframe

前端 未结 2 1910
醉酒成梦
醉酒成梦 2021-01-03 04:51

I am trying to intercept links clicked on a page including those inside an iframe. This is the code that I have but it is not working. Any ideas what I need to do?

相关标签:
2条回答
  • 2021-01-03 05:44

    You need to reach inside the <iframe> and set the delegate there, you can do it like this:

    $('#myiframe').contents().find("#container").delegate('a', 'click', function(e){ 
      //do stuff
    }
    

    Edit - Mailslut makes a good points below, if the iframe isn't on the same domain (and port), you can't do anything like this. If that's the case and you want to know more about why, read about the same-origin policy there for security reasons.

    0 讨论(0)
  • 2021-01-03 05:46

    Why not add the event listener inside the iframe and then call the parent / opener to notify the event.

    If you the contents of the iframe is on a different domain, you won't be able to perform this as it is classed as "click-jacking", which was a big security threat.

    0 讨论(0)
提交回复
热议问题