I have an iframed page on my website. The iframed page has links that have set target="_top" and so they open over my website.
I want all the links in the iframed page to open in a new window. Can I do that?
| SitePoint Sponsor |


I have an iframed page on my website. The iframed page has links that have set target="_top" and so they open over my website.
I want all the links in the iframed page to open in a new window. Can I do that?


Why not? Any reason you can't simply remove the target attribute from your framed pages?Originally Posted by IamAdam


No, the thing is, the page I am framing is not mine - I don't have control over it.


I see. Then I guess you should explain your problem to the page's author and ask them if they would remove them. I can't think of a reason why they shouldn't as long as you can explain yourself clearly enough.



Perhaps this may work - I adapted it a little, normally I used this to mak links opening in a new window without the target-attribute hardcoded - instead I used rel="external" and set the target with JS.
Try it and let me know if itworks
Code:function externalLinks() { if(!document.getElementsByTagName) { return false; } var anchors = document.getElementsByTagName("a"); for(var i = 0; i < anchors.length; i++) { var anchor = anchors[i]; if (anchor.getAttribute('href') && anchor.getAttribute('target') && anchor.getAttribute('target') == '_top') { anchor.target = '_blank'; } } } window.onload = externalLinks;


Thanks for the code, and sorry for my belated reply.
The page in the iframe does not actually have hardcoded target. The iframed page contains javascript links which are set to open in "_top"
However, there is nothing on the iframed page that has target="_top". Only javascript which contains links that open in "_top"
To make this look more imaginable, I am framing a javascript adcode.

You can't use javascript to update a web page from another site that is displayed in an iframe. Whatever is in the iframe works the way the owner of that page intends it to. In the case of ads, making any such change would violate the terms of service for your ad account if this were possible.
Stephen J Chapman
javascriptexample.net, Book Reviews, follow me on Twitter
HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
<input name="html5" type="text" required pattern="^$">
Bookmarks