javascript – Link of an Iframe opening in new tab?

Question:

Is there a possibility that when a person clicks on a link in an iframe , that link opens in another tab?

I don't have control of the other page's HTML, so I can't put the _blank in the link from the HTML.

Answer:

If the pages are on the same domain:

Yes, it is possible. Just add target="_blank" to 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 the page due to same-origin security policy .

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

Scroll to Top