Open links from iframe

Asked

Viewed 1,407 times

2

How to prevent Iframe links that have the property target="_blank" and open these links in another iframe only on the same page?

Example:

Content of Iframe1: <a href="http://t.co/YJdEOEu8Dz" target="_blank">MEU VIDEO</a> Content of Iframe2: <span>Aqui será o link que foi clicado no iframe1<span>

  • These iframes are in the same domain?

1 answer

2


Would that be... (but you’ll have to trade http://www.google.com because it will not load inside the Iframe.). I was able to find a Google URL that works inside Iframe, just so you can check how it works.

$("body").on("click", "a", function(e) {
  var target = $(this).attr("target");
  if (target == '_blank') {
    $("#meuIFrame").attr('src', $(this).attr("href"));
    return false;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <a href="http://www.google.com/custom?q=&btnG=Search" target="_blank">Google</a>
</div>
<iframe id="meuIFrame" src="" style="width: 200px; height: 200px;"></iframe>

Such as the URL http://google.com does not work, any URL whose response contains the header X-Frame-Options: SAMEORIGIN also will not open inside Iframe, unless you have how to put this HTML in the same source as the URL.

  • yes the 2 iframe are located on the same page

  • type I am using this to run a twitter widget but when it is clicked to Retwittar or Like it opens on new page. so I want to have these 2 iframe 1 with the user’s twitter and the other to open when clicking the links of the post!

  • But just one thing... I don’t want to open all the target links="_Blank" no, just what’s inside the twitter widget.

  • 1

    In that case, just replace the "body" by another selector involving only the desired widget.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.