0

I have this code I got from a site. It works fine and it can detect a click inside of an iframe.

$('body', $('#iframeId1').contents()).click(function(event) {
 alert("Link clicked");
});

However, I am trying to get the href of the clicked link. I tried :

var href = $(this).attr("href");
  alert(href);

But this alerts undefined

1 Answers1

-1

If you want to bind all links (a tags) in your body, you want to do something like this. This will work as expected:

$('body').on('click', 'a', function(event) {
  alert("Link clicked");
  var href = $(this).attr("href");
  alert(href);
});

However, you won't be able to access or bind the contents of an iframe (see this question), since that would be a huge security problem.

Community
  • 1
  • 1
Jorge Silva
  • 4,574
  • 1
  • 23
  • 42