Dynamically targeted links

In the previous post, I quickly created a widgetized blog feed. However, the dynamically generated HTML included links that would open in the same window that the links was on. Under normal circumstances, this is the best user experience. However since Clearspring generates HTML-based feeds in an iframe, clicking on any of the links in the widget caused the page to be rendered in the small, confined space of 300×236 pixels.

I know I could have used document.getElementsByTagName('a') but I was wondering if there was a nicer way to grab all the links on a page. Googling around pointed me to this post. I can get all the links on a page by using document.links, which returns an array of the links on a page as DOM objects. I haven’t tested this on all browsers, but that’s pretty sweet.

Here’s the code in it’s entirety to make all links on a page open up in a new window:

<script type="text/javascript">
var links = document.links;
for( var i = 0 ; i < links.length ; i++ ) {
   links[i].setAttribute('target','_blank');
}
</script>
Dynamically targeted links

Leave a Reply

Your email address will not be published. Required fields are marked *