[RESOLVED]Force all links in Iframe to be open in new tab

Hi,

I have a html file that contains some <a> tag with no target attribute define.

I have a iframe with following properties

<iframe id="ifr" runat="server" scrolling="yes" width="100%" frameborder="0" class="My_IFR" marginheight="0" marginwidth="0">

I am setting the src for iframe on gridview row click dynamically.

Can i do something so that <a> tag to be open in new tab.

I have html files in the same domain as the application.

You can set the target to _blank, depending on the browser you are using it should open in a new tab but some will open as a new window.

Hi @breath2k

I have no control over html file. i just have to show it in iframe. Can i do it by using jquery. I have search through the net all the day but no luck !!!

Does the a tag have any kind of class or id on it, if yes then you can use jQuery:

// if has an id
$("#atagid").attr("target","_blank");

// if more than one with a class
$(".atagclass").each(function() {
    $(this).attr("target","_blank");
});

Although not sure if it will change a tags within the iFrame until you test it.

If the iframe’s source is on a domain other than your own, there will be a case of cross-site scripting (XSS), which most (all?) browsers will block.
In that case the above javascript won’t work. Otherwise it should be fine.

Leave a Reply