If all my dire warnings the other day didn’t scare you off, and after careful consideration you’ve found yourself with a compelling enough reason to open a new window — compelling enough that your users will expect a window to open, which I think is quite a rare situation — please don’t use the old HTML target="_blank" attribute. Use JavaScript.

For one thing, the target attribute is not available in XHTML (you are using XHTML, right?) because it is not structural: it is behavioral. Page behavior should be controlled (or influenced, at least) with means other than markup, e.g. JavaScript or CSS. The one exception is framesets, where target does have a structural meaning and so is included in the XHTML 1.0 Frameset DTD. But you’re not using frames, are you?

If you find yourself in a position where you have no choice but to open a new window, please don’t do this:

<a href="javascript:openWindow('new.html')">DO NOT DO THIS</a>

do this instead:

<a href="new.html" onclick="openWindow(this.href); return false;">new window</a>

(This assumes that you’ve defined an openWindow() function.)

The second way, even those user agents with JavaScript disabled or unavailable can still access the page to which you’re sending them. Too, those of us who follow links by dragging them to another window or using contextual menus to open new windows or tabs can follow the link using our preferred method.

Thank you. You’ve just made the Web a better place.

You’d make it an even better place if you never opened new windows in the first place.

Update: Paul Sowden wrote to point out although Mozilla allows the blocking of target="_blank", it’s harder to block links using the JavaScript technique that I suggest. Using JavaScript, he argues, reduces the user’s control. Perhaps, but this is an issue that needs to be resolved by Mozilla’s developers.

Remember, in XHTML, JavaScript is the only way to open a new window. I agree wholeheartedly that opening new windows is something that you should studiously avoid, but sometimes you just can’t help it. Perhaps your boss or your kidnapper is staring over your shoulder until you comply with her demands. In that case, I humbly request that you write your JavaScript code in a way that allows for the greatest amount of freedom and control for the end user.

I’ve thrown together a quick demonstration of how it works. Disable JavaScript, play with your browser preferences, see how it all comes together. If you have improvements, please share.

It’s possible to disable all window-opening behavior in Mozilla, by the way. Add this line to your user preferences file:

user_pref("capability.policy.default.Window.open","noAccess");

You’ll still be able to open windows if you want to, but it won’t happen without your permission.

This and other goodies are documented on or near the end-user documentation on the Mozilla site (Customizing Mozilla, Hidden Mozilla Prefs).

I hope that before long, disabling “Open a link in a new window” in the Mozilla GUI-defined preferences also applies to JavaScript-opened windows.