0

I am loading a url using a iframe and when a user click on any link within the iframe, i am trying to get that href source and append to a url and redirect it.

<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>

<a href="http://www.google.com" >Google</a><br/>

<iframe src="http://example.com" width="100%" height="700px"></iframe>

</body>
</html>

Using below javascript code(found on stack overflow), it is redirecting the links on web page(Ex; google link in the page) but its not redirecting the links in the iframe.

    $('a').click(function (event) {
      event.preventDefault();
      var href = $(this).attr('href')
      window.location='http://someurlhere?link=' + href;
    });

How to do get the link source from the Iframe and append it to a url and redirect it?

RSD
  • 55
  • 1
  • 8
  • 2
    What you're trying to do is not possible as the content of the iframe is coming from a third-party domain. If you own both domains then you could potentially use [`postMessage()`](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage), although I'd argue there's far better ways to achieve whatever it is you're trying to do without resorting to that – Rory McCrossan Apr 17 '20 at 11:47
  • Its a third party domain. what is the best way to get what i am trying? is there any other solution for this? – RSD Apr 17 '20 at 11:55
  • Then no, there's nothing at all you can do. This behaviour is blocked for security reasons. Imagine how easy it would be to spoof/phish if you could actually do what you're asking. – Rory McCrossan Apr 17 '20 at 11:56
  • Yes i agree with you, but i am trying to just show the content of the page. They should bring some changes (Allowing to show content etc.) – RSD Apr 17 '20 at 12:00
  • It's never going to happen. It would be far too easy to exploit. – Rory McCrossan Apr 17 '20 at 12:01

0 Answers0