17

I'm trying to make it so when you click on a link in an <iframe> it will open the link in a new tab - not in the <iframe>. Please note I do not have control of the content the people are viewing through the <iframe>.

I'm trying to do this with javascript possibly, like $('a').setAttribute('target','_blank'); but it's not working out for me. I also have in my HTML document <base target="_blank">, that is not doing the trick too.

Is this impossible? Or am I just searching in the wrong places? Thanks in advance!

st mnmn
  • 3,555
  • 3
  • 25
  • 32
Darryl Huffman
  • 2,499
  • 3
  • 18
  • 40

6 Answers6

31

To load all links on the page in the parent window use:

<base target="_parent" />

else in a new window use:

<base target="_blank" />
ronish
  • 528
  • 1
  • 4
  • 6
11

I'd expect this :

$('a').setAttribute('target','_blank');

.. to fail (silently - because jquery generally fails silently) because, if the content of the iFrame is from a different Domain, there are access issues with manipulating the page within the iFrame from the containing page.

That is : If your own page is from domain a (eg mysite.com), and the iframe is from domain b (eg someothersite.com), then web browser security behaviour is such that using javascript to manipulate the iFrame content will give an "Access is denied" error.

If you were to use non-jquery javascript you'll see the error. From the parent page, something likie this :

window.frames["iFrameName"].getElementsByTagName("a")[0].target="_blank"

you'll see the error.

Unfortunately I'm not sure what you can do about this. It's a deliberate thing to stop one website including another, and changing the content after loading so that it says something else.

One way around it would be to call a script on your server (ie same domain) and pass in a URL. The script gets the content of the page you were after and regurgitates it to your browser, so you'll have the content of your desired webpage but the address of it was within your site.. eg if it were php:

http://yourdomain.com/getURL.php?url=http:www.google.com

Watch out for issues with authentication there though

user2808054
  • 1,376
  • 1
  • 11
  • 19
3

check this solution it helped me: Make links inside an iframe open in a new window

or you can try this code: parent.window.open("{{your location}}");

Community
  • 1
  • 1
Liad Livnat
  • 7,435
  • 16
  • 60
  • 98
2

using javascript

var links= document.getElementsByTagName('a');
for (var i=0; i<links.length; i++){
   links[i].setAttribute('target', '_top');
}

using jquery

$('a').each(function() {
   var a = new RegExp('/' + window.location.host + '/');
   if (!a.test(this.href)) {
       $(this).attr("target","_top");
   }
});
Ratan Paul
  • 484
  • 9
  • 25
1

Replace target='blank' with onclick='window.open();'

Tarabass
  • 3,132
  • 2
  • 17
  • 35
alice
  • 11
  • 1
  • Lol, that's the only thing that actually worked from all other solutions I have tried. Thank you very much! – odedta May 18 '16 at 10:43
0

Use $('a').attr('target','_blank'); instead of setAttribute...

FIDDLE

Karim AG
  • 2,166
  • 15
  • 29
  • 1
    You are just telling him to use different JavaScript notation. How does this solve the question at all? This would be better served as a comment as opposed to an answer. – AlbatrossCafe Aug 25 '15 at 21:08
  • I upvoted because it does fix the proposed JS. OP had two problems. – Citizen Feb 09 '16 at 18:09