JQuery - intercepts links clicked inside an iframe

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

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

A container is a div identifier directly inside an iframe.

Thanks in advance for any advice.

+4
source share
2 answers

You need to get 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 gives good points below, if the iframe is not in the same domain (and port), you cannot do anything about it. If this is the case and you want to learn more about why, read about policies of the same origin for security reasons.

+2
source

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

If the contents of the iframe are in a different domain, you will not be able to accomplish this, since it is classified as click-to-click, which poses a great security risk.

+1
source

Source: https://habr.com/ru/post/1308347/


All Articles