iamcal.com

[home] [PDF Version]

Blog Hacks : Opening links in a new window

Originally published 20th June 2003

The usual rule for accessability purposes is to not open links in a new window - it's confusing for users, breaks the "back" functionality and is unescessary - users can explicitly choose to open a link in a new window by holding down the shift (or apple) key while clicking, but cannot choose not to open a new window for links with a target attribute. So specifiying target attributes is generally a bad thing.

But what if you're running a weblog, where your links take users off-site? Some users like having new windows for each link and don't want to (or don't know how to) do it themselves. Luckily, help is at hand. With a small piece of javascript you can offer your users the option of whether links should open in the current window or open a fresh window each time.

First you'll need to grab the javascript, which you can get from http://www.iamcal.com/files/new_links.zip. You'll need to tell your pages to load this file by putting in the following code. It should be inserted just before your closing body tag (</body>):

	<script language="JavaScript" src="new_window.js"></script>

The only thing left to do is to add the checkbox to let users select whether they want new windows enabled. In the place on you r page where you would like the control to appear, put the following HTML:

	<form onsubmit="return false;">
	<input type="checkbox" name="new_window_checkbox" id="new_window_checkbox" 
		onclick="set_link_targets(this.checked);">
		<label for="new_window_checkbox">open all links in new windows</label>
	</form>

The script is pretty intelligent too - it users cookies to remember users' settings, so if a user checks the checkbox, it will remain checked when they come back to your site next time.