A List Apart

Issue № 174

Accessible Pop-up Links

by Published in HTML, JavaScript, Accessibility, Usability130 Comments

Sometimes we have to use pop-ups — so we might as well do them right. This article will show you how to make them more accessible and reliable while simplifying their implementation.

For the purposes of this article, pop-up links are links that open in a new window, and that rely on JavaScript to do so. Ordinarily, all you need for a link to open on a new window is to set its “target” attribute to “_blank”. With pop-up links, JavaScript is used either because special window properties must be set, or because the DOCTYPE in place does not allow the “target” attribute.

Links 101

The concepts here are very simple, but let’s get some terminology clear before we continue:

We’ll be using here the HTML “a” element and dealing with its “href” and “target” attributes. We’ll also use the “window.open” JavaScript method and refer to its arguments “url,” “name,” and “features”. Below is a table describing these items and their equivalence between HTML and JavaScript.

Concepta attributewindow.open parameterDescription
Link destinationhrefurlThe URI of a resource to be loaded
Target windowtargetnameAn identifier of the window where the resource should load
FeaturesN/AfeaturesChrome/UI properties of the target window

The bad

First, let’s put an end to the problem with most JavaScript pop-up links: their HTML. Here’s a representative sample:

<a href="[removed]
  void(0)">pop me up

This piece of HTML is the root of all that’s unholy in the pop-up world. It sports two serious usability issues that would otherwise be easily avoidable. The first one should be clear: users without JavaScript are unable to follow the link.

The reason why the link won’t load for these users is simple, and stupid: the link destination is set to an action (a JavaScript function), rather than to its actual destination. Depressingly, the purpose of this function is to load the actual link destination.

The ugly

The second issue is not so obvious, but comes with its own interesting irony: the link will fail for the very people who concur with the designer’s decision: users who already planned to open the link in a new window.

When I intentionally open a link in a new tab or window, here’s a simplified description of what the browser does: it grabs the value specified in the link’s “href” attribute and it sets it as the location for the new tab or window.

If JavaScript is disabled, we reach the same issue described before. If JavaScript is enabled, however, the browser will call the pop-up function within the scope of the new window’s location bar — and it’ll fail, because the function only exists within the scope of the original page. In other words, when you open a new window and it tries to run that JavaScript function, it will fail because the new window has never heard of that JavaScript function.

A place for everything…

You’ll notice that both problems stem from the same mistake: they specify something that’s not the link’s destination as the link’s destination.

A better approach would be to specify the link’s destination correctly in the “href” attribute, and use an event handler (which is where you place JavaScript code) to open the new window and set its special properties.

Here’s a simple approach:

<a href="http://example.com">pop me up

We can avoid redundancy by passing the element to the function and having it read the URL from the “href” attribute:

<a href="http://example.com">pop me up

The former example is still useful when you want to provide a separate URL for users without JavaScript. Keep in mind that the URL that will open for users who manually launch the link in a new window will be the one specified in the “href” attribute, not the one passed via script. (You can always use a JavaScript redirect in that page.)

If you were hoping to see some kickass JavaScript, hold on. For now, we’re not interested in the actual JavaScript implementation, we’re just trying to define the best HTML code. Either example above would already solve the accessibility problems because the “href” attribute contains the actual link destination. It’s good enough, but we can make it better.

Do it with HTML

A guideline I use while coding is that I should only script what HTML cannot handle. If it can be done in HTML, then it should be. JavaScript should come as a complement.

So can HTML follow a link? Yes. Can HTML make this link load in a new window? Yes. (With transitional DOCTYPEs at least.) Can HTML determine the chrome features of this window? No. This is where JavaScript should come in.

So here’s our improved HTML code:

<a href="http://example.com"   target="_blank">pop me up

By adding the “target” attribute we make sure that the link will open in a new window even if the user doesn’t have JavaScript enabled, although the window’s features won’t be set.

A note for accessibility aficionados

Our events will not trigger exclusively for mouse users despite the fact that we’re only capturing “onclick”. “Onclick” is triggered by all forms of activation (e.g. pressing enter) on all popular browsers. (It works pretty much the same way as Mozilla’s XUL’s oncommand.)


Now that our HTML is tidy, let’s get our hands dirty with the JavaScript implementation. I’ll spare you the simple one-liner (that would be a dull window.open wrapper) and provide you with some full-featured pop-up handling code:

';function raw_popup(url, target, features) {
  if (isUndefined(features)) {
    features = _POPUP_FEATURES;
  if (isUndefined(target)) {
    target = '_blank';
  var theWindow =
    window.open(url, target, features);
  return theWindow;
}function link_popup(src, features) {
    src.getAttribute('target') || '_blank',

There’s a lot of stuff here, so let’s take a closer look:

raw_popup is a fancy “window.open” wrapper with a couple of things to make our life easier:

  • Better defaults: if no target window is provided, it’ll default to _blank. If no features are specified, it’ll default to the global constant _POPUP_FEATURES.
  • Focus: if the target window is already open but in the background, it’s brought to the front. The target window may be open in the background if we are using named windows (by specifying a target other than _blank), and sending more than one link to the same target. It’s good usability to focus the window because otherwise the user would get no apparent feedback for clicking the link.

link_popup was created specifically to open new windows from links. It grabs the URL from the anchor tag’s href attribute and optionally grabs the target from the target attribute. If the latter is not provided, it’ll default to _blank. This makes the function compatible with strict documents while allowing usage of named target windows. You should always specify a target in the HTML code when using a transitional DOCTYPE, to aid users without JavaScript. It’s good practice to write functions that return something, and nothing is more natural than returning the newly opened window on both cases, same as “window.open” does.

The isUndefined function in the code above is a function from my personal library. We’ll use a lot of code from this library hereinafter, and a trimmed-down version is provided at the end of the article and documented in the source. I find it very useful and I hope it aids your future scripting beyond simply popping up windows.

If all you need to do is pop up a couple of windows on a page or two, then this should be more than enough for you. If you plan to make heavy use of them (e.g. for an image gallery or maybe a CMS) and are not intimidated by JavaScript or the DOM, please read on.

Separate logic and presentation

You probably hear it more than you’d like to: it’s important to separate logic from presentation. It’s true. In addition to a warm, fuzzy feeling, it gives you code that is easier to maintain.

So let’s take a look back into that HTML. There’s something in it that doesn’t belong there. What could possibly not belong to HTML? JavaScript! Out with the onclick thingie:

<a id="my-popup-link"
  target="_blank">pop me up

Now we must add event listeners to the “a”s that will pop up. To attach the event listener via JavaScript we add the following code to our script. Our script should be in the document head (either in an external script or in a script block):

function event_popup(e) {
}listen('load', window, function() {
  listen('click', 'my-popup-link', event_popup);
  // ... other onload statements

If we want to add an event listener to an element in the document, the element must be present in the browser’s internal document tree. When the document is finished loading, it triggers an event, load, on the window object. At this point, we’re sure the tree is complete and all elements are present. Here, we use listen to assign a function to this event, which will then call listen to safely assign event handlers to document nodes.

Listen is a cross-browser implementation of the DOM’s addEventListener(). It takes three parameters: event, elem, and func. To determine elem, it uses getElem, also from my library. getElem is a shorthand for document.getElementById with a special feature: it can take the element’s id, or it can take the element itself. It returns the element on both cases. Thus, elem can be either a string or an Element.

You’ll notice that we’re calling e.currentTarget and e.preventDefault, which IE doesn’t implement. A very useful feature of listen is that, in IE, it’ll wrap func and attach this wrapper to the event. This wrapper calls func passing it a faux Event object which will mimic these two features for IE.

Going wild with the DOM

Adding a listener to an element via id is ok when you have very few links to pop up. If you have lots of links, it gets boring to code each listener one by one. For that reason, I present you with mlisten.

mlisten is the same as listen, but it takes an element list instead of an element, and adds the listener to every element on the list. The list can be a NodeList, or an Array of id strings and/or Element objects.

Here’s an example of using mlisten to pop up all links in a list:


If your links are not structurally grouped, you can select them by their class using my function getElementsByClass(className [, tagName [, parentNode]]):


The function handles elements with multiple space-separated classes well, so you can safely use one class to style the element and another class to denote behavior.

Remember, these calls should be made on the window’s onload event.

One small problem

By using listeners, we’re unable to provide parameters to the functions. That keeps us from passing in individual window features to each link. Right? Not quite. Here’s a quick solution:

function event_popup_features(features) {
  return function(e) { 
    link_popup(e.currentTarget, features); 
}// onload…

There’s an important distinction to make here. Say fn is a function. When you write fn you have a reference to fn. When you write fn() you are calling fn and you have whatever it returns.

Here we’re using a function that returns a function. We’re not assigning event_popup_features to the event, we’re calling it and assigning the function that it returns. This function is like a personalized version of event_popup that takes into account the features we provided when calling event_popup_features.

The source

That pretty much sums it up. Now that you know more ways to pop up a link than you ever wanted or needed to, feel free to grab the source code and the examples:

About the Author

130 Reader Comments

Load Comments