Click here to Skip to main content
Licence CPOL
First Posted 7 Jan 2008
Views 12,810
Bookmarked 10 times

Force all link tags to target a new window (Handy for RSS feeds)

By | 7 Jan 2008 | Article
A link rewrite script

Introduction

This handy piece off code will rewrite all the (a href) links to target a new window this is very handy in the case of rss feeds nested within iframes in your applications where the code returned by the feed does not always target a new window

Background

A while back i had to insert a news feed into my ajax enabled website and due to the nature of my ajax engine i had to create a iframe (containing my feed) inside a div everytime the ajax event ended, problem was that the links returned from FT.com had one link that caused the page in the iframe to load their home page in the frame when clicked, there was no way to alter the links using target because these links get returned from another server so i came across a handy little script i had to change a bit to do exactly what needed to be done

Using the code

put this code in script tags just underneath the body tag of the page you want to nest in the iframe

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Chona1171

Web Developer

South Africa South Africa

Member

Hi I am a Software Developer, I have studied, Comprehensive programming, Software Development, Business and Project Management.
 
After my First year of studies I recieved a full bursary for my second year and worked as a Junior Software Development Instructor,
I am skilled in a vast array of languages my top languages being Java (SE,ME,EE),SQL C#, VB.Net, VB 6 & Asp.Net,

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberMark Nischalke7:38 5 Dec '08  
GeneralWorking form of above code Pinmembermr.malik.cm2:09 11 Aug '08  
GeneralRe: Working form of above code PinmemberChona11714:00 11 Aug '08  
GeneralRe: Working form of above code Pinmembersohighthesky3:02 27 Jun '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 8 Jan 2008
Article Copyright 2008 by Chona1171
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid