Iframe link opening in new tab?

Asked

Viewed 1,029 times

3

There is the possibility when the person clicks on a link in a iframe, this link opens in another tab?

I don’t have control of the HTML of the other page, so I can’t put the _blank at the link by HTML.

1 answer

0


If pages are in the same domain:

Yes, it is possible. Just add the target="_blank" all links. Example with jQuery:

$(document).ready(function () {
    $("iframe").load(function () {
        var iframe = document.getElementById("iframe")
        var anchors = iframe.contentDocument.getElementsByTagName("a");
        for (var i in anchors) {
            anchors[i].setAttribute("target", "_blank");
        }
    });
});

If they are in different domains:

Unable to manipulate page due to security policy of the same origin.

However, you can do a trick, creating a proxy that provides a mirror of the original page in your domain. This can only be done with server-side languages.

  • Hello @rodorgas, just to add content to your answer, this case of policy of the same origin not only refers to situations of equal domains, there are other criteria that are analyzed, worth taking a look at this contents.

Browser other questions tagged

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