where's the code?

7.3.02: In an outstanding series on weblog usability (30 days to a more accessible weblog) Mark Pilgrim argues convincingly that web designers should not have links open new windows by default. I agree without reservation.

5.27.02: Apparently Jason Francis (jasonledoux@msn.com) was generous enough to submit our script (sans proper credit) to javascript.internet.com: "Add the option of allowing links on your page to be opened in a new window with this powerful script. A simple checkbox toggles between the current and new window. Great!" The Javascript Source: Forms: Open Link. Thanks Jason! Great!

5.12.02: If you use the popular YACCS comment system on your weblog, take a look at the code Brendan posted near the bottom of this YACCS support discussion. Using Brendan's code will prevent the issue where two windows can open when a comment link is clicked.

4.19.02: I just discovered this thorough explication of several methods of providing user control over link targets at evolt: User-Defined Window Targeting w/JavaScript. The author was apparently unaware that I'd already written the simplest version of this feature. The article shows a version using radio buttons to choose between "self" "same" and "new", which is a nice alternative. There is also discussion of preventing internal links from opening windows, and of a method by which the page author can specify exactly to which links the new window option should apply. I like that idea, but you'd want to make it clear to the user which links would be affected -- perhaps too complicated for most situations.

4.16.02: If you've had trouble using my links code with any of the many weblog comments systems which use javascript to pop open a comments window, here is a YACCS discussion which may help you get it working.

2.07.02: Ian Scott-Parker said some awfully nice things about me. Thanks.

12.19.01: Matt Sawkill has written two new bookmarklets, based on my code, and in response to a cardhouse suggestion: "Browsers need two more buttons: 'open up every link on this page' and 'open up every link on this page that isn't from this website.'"

10.27.01: Freakho has improved upon my bookmarklet with one that ignores local, email, and javascript links. Hmm, email. I need to add that to the improved version I'm running on randomWalks don't I? Here's the improved new windows bookmarklet.

7.30.01: You might want to take a look at what Jerry Kindall has done with this idea. He's got checkbox based on a tight bit of JavaScript that sets a cookie to store the user's choice, and his code only modifies external links, which will please a lot of people. It looks like a nice evolution of the feature.

9.11.00: Blogger has posted a "How to toggle new window linking" how-to describing how to use this code. Cool.

5.24.00: I posted my bookmarklet to MetaFilter.




If you want to add this feature to your Web site, use the following code. It is customary (though not mandatory) to put it in the <head> of your page:
<script language="JavaScript"><!--
	// thanks to randomwalks.com for this code
	function targetLinks(boNew) {
		if (boNew) 
			where = "_blank";
		else
			where = "_self";
		for (var i=0; i<=(document.links.length-1); i++) {
			document.links[i].target = where;
		}
	}
	// -->
</script>
	
Use this code in the <body> of your page, wherever you want the checkbox to appear:
<form name="targeter">
<input type="checkbox" name="targetbox" id="tcheck"
    onclick="targetLinks(this.checked);">
<label for="tcheck" style="cursor: hand;">
    links open windows
</label>
</form>
	

I've also turned this code into a "links open windows" bookmarklet. (What's a bookmarklet?) You can drag the "links open windows" link to your toolbar and click it from any page to cause links to spawn windows. The converse, forcing links to one window, can sometimes be useful.

If you use the code, please leave a comment with it pointing to randomWalks, and if you modify it in an interesting way, please let me know. Enjoy.

What's new on randomWalks?