Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
162
rated 0 times [  163] [ 1]  / answers: 1 / hits: 73382  / 10 Years ago, wed, april 2, 2014, 12:00:00

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!


More From » jquery

 Answers
34

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


[#71661] Tuesday, April 1, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
herman

Total Points: 110
Total Questions: 90
Total Answers: 108

Location: Bosnia and Herzegovina
Member since Thu, Jun 24, 2021
3 Years ago
;