Accessible Pop-up Hyperlinks – A Record Aside

Typically we’ve got to make use of pop-ups — so we’d as nicely do them proper. This text will present you easy methods to make them extra accessible and dependable whereas simplifying their implementation.

Article Continues Under

For the needs of this text, pop-up hyperlinks are hyperlinks that open in a brand new window, and that depend on JavaScript to take action. Ordinarily, all you want for a hyperlink to open on a brand new window is to set its “goal” attribute to “_blank”. With pop-up hyperlinks, JavaScript is used both as a result of particular window properties have to be set, or as a result of the DOCTYPE in place doesn’t enable the “goal” attribute.

The ideas listed here are quite simple, however let’s get some terminology clear earlier than we proceed:

We’ll be utilizing right here the HTML “a” factor and coping with its “href” and “goal” attributes. We’ll additionally use the “” JavaScript technique and check with its arguments “url,” “identify,” and “options”. Under is a desk describing this stuff and their equivalence between HTML and JavaScript.

















Idea a attribute parameter Description
Hyperlink vacation spot href url The URI of a useful resource to be loaded
Goal window goal identify An identifier of the window the place the useful resource ought to load
Options N/A options Chrome/UI properties of the goal window

First, let’s put an finish to the issue with most JavaScript pop-up hyperlinks: their HTML. Right here’s a consultant pattern:

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

This piece of HTML is the basis of all that’s unholy within the pop-up world. It sports activities two critical usability points that may in any other case be simply avoidable. The primary one must be clear: customers with out JavaScript are unable to observe the hyperlink.

The explanation why the hyperlink received’t load for these customers is straightforward, and silly: the hyperlink vacation spot is ready to an motion (a JavaScript operate), reasonably than to its precise vacation spot. Depressingly, the aim of this operate is to load the precise hyperlink vacation spot.

The second concern just isn’t so apparent, however comes with its personal attention-grabbing irony: the hyperlink will fail for the very individuals who concur with the designer’s resolution: customers who already deliberate to open the hyperlink in a brand new window.

Once I deliberately open a hyperlink in a brand new tab or window, right here’s a simplified description of what the browser does: it grabs the worth specified within the hyperlink’s “href” attribute and it units it as the situation for the brand new tab or window.

If JavaScript is disabled, we attain the identical concern described earlier than. If JavaScript is enabled, nevertheless, the browser will name the pop-up operate inside the scope of the brand new window’s location bar — and it’ll fail, as a result of the operate solely exists inside the scope of the unique web page. In different phrases, while you open a brand new window and it tries to run that JavaScript operate, it should fail as a result of the brand new window has by no means heard of that JavaScript operate.

A spot for all the things…#section5

You’ll discover that each issues stem from the identical mistake: they specify one thing that’s not the hyperlink’s vacation spot because the hyperlink’s vacation spot.

A greater strategy can be to specify the hyperlink’s vacation spot accurately within the “href” attribute, and use an occasion handler (which is the place you place JavaScript code) to open the brand new window and set its particular properties.

Right here’s a easy strategy:

<a href="">pop me up

We are able to keep away from redundancy by passing the factor to the operate and having it learn the URL from the “href” attribute:

<a href="">pop me up

The previous instance continues to be helpful while you wish to present a separate URL for customers with out JavaScript. Remember the fact that the URL that may open for customers who manually launch the hyperlink in a brand new window would be the one specified within the “href” attribute, not the one handed through script. (You may all the time use a JavaScript redirect in that web page.)

For those who have been hoping to see some kickass JavaScript, maintain on. For now, we’re not within the precise JavaScript implementation, we’re simply attempting to outline one of the best HTML code. Both instance above would already resolve the accessibility issues as a result of the “href” attribute comprises the precise hyperlink vacation spot. It’s adequate, however we will make it higher.

A suggestion I take advantage of whereas coding is that I ought to solely script what HTML can’t deal with. If it may be performed in HTML, then it must be. JavaScript ought to come as a complement.

So can HTML observe a hyperlink? Sure. Can HTML make this hyperlink load in a brand new window? Sure. (With transitional DOCTYPEs a minimum of.) Can HTML decide the chrome options of this window? No. That is the place JavaScript ought to are available in.

So right here’s our improved HTML code:

<a href=""   goal="_blank">pop me up

By including the “goal” attribute we make it possible for the hyperlink will open in a brand new window even when the consumer doesn’t have JavaScript enabled, though the window’s options received’t be set.

A word for accessibility aficionados#section7

Our occasions won’t set off solely for mouse customers even if we’re solely capturing “onclick”. “Onclick” is triggered by all types of activation (e.g. urgent enter) on all well-liked browsers. (It really works just about the identical approach as Mozilla’s XUL’s oncommand.)

Now that our HTML is tidy, let’s get our palms soiled with the JavaScript implementation. I’ll spare you the straightforward one-liner (that may be a boring wrapper) and offer you some full-featured pop-up dealing with code:

';operate raw_popup(url, goal, options) {
  if (isUndefined(options)) {
    options = _POPUP_FEATURES;
  if (isUndefined(goal)) {
  var theWindow =, goal, options);
  return theWindow;
}operate link_popup(src, options) 

There’s quite a lot of stuff right here, so let’s take a more in-depth look:

raw_popup is a flowery “” wrapper with a few issues to make our life simpler:

  • Higher defaults: if no goal window is offered, it’ll default to _blank. If no options are specified, it’ll default to the worldwide fixed _POPUP_FEATURES.
  • Focus: if the goal window is already open however within the background, it’s delivered to the entrance. The goal window could also be open within the background if we’re utilizing named home windows (by specifying a goal aside from _blank), and sending a couple of hyperlink to the identical goal. It’s good usability to focus the window as a result of in any other case the consumer would get no obvious suggestions for clicking the hyperlink.

link_popup was created particularly to open new home windows from hyperlinks. It grabs the URL from the anchor tag’s href attribute and optionally grabs the goal from the goal attribute. If the latter just isn’t offered, it’ll default to _blank. This makes the operate suitable with strict paperwork whereas permitting utilization of named goal home windows. You must all the time specify a goal within the HTML code when utilizing a transitional DOCTYPE, to help customers with out JavaScript. It’s good observe to put in writing features that return one thing, and nothing is extra pure than returning the newly opened window on each circumstances, similar as “” does.

The isUndefined operate within the code above is a operate from my private library. We’ll use quite a lot of code from this library hereinafter, and a trimmed-down model is offered on the finish of the article and documented within the supply. I discover it very helpful and I hope it sida your future scripting past merely popping up home windows.

If all that you must do is pop up a few home windows on a web page or two, then this must be greater than sufficient for you. For those who plan to make heavy use of them (e.g. for a picture gallery or perhaps a CMS) and usually are not intimidated by JavaScript or the DOM, please learn on.

Separate logic and presentation#section9

You in all probability hear it greater than you’d wish to: it’s necessary to separate logic from presentation. It’s true. Along with a heat, fuzzy feeling, it offers you code that’s simpler to take care of.

So let’s have a look again into that HTML. There’s one thing in it that doesn’t belong there. What may presumably not belong to HTML? JavaScript! Out with the onclick thingie:

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

Now we should add occasion listeners to the “a”s that may pop up. To connect the occasion listener through JavaScript we add the next code to our script. Our script must be within the doc head (both in an exterior script or in a script block):

operate event_popup(e) {
}pay attention('load', window, operate() {
  pay attention('click on', 'my-popup-link', event_popup);
  // ... different onload statements

If we wish to add an occasion listener to a component within the doc, the factor have to be current within the browser’s inner doc tree. When the doc is completed loading, it triggers an occasion, load, on the window object. At this level, we’re certain the tree is full and all components are current. Right here, we use pay attention to assign a operate to this occasion, which is able to then name pay attention to securely assign occasion handlers to doc nodes.

Pay attention is a cross-browser implementation of the DOM’s addEventListener(). It takes three parameters: occasion, elem, and func. To find out elem, it makes use of getElem, additionally from my library. getElem is a shorthand for doc.getElementById with a particular function: it might take the factor’s id, or it might take the factor itself. It returns the factor on each circumstances. Thus, elem will be both a string or an Factor.

You’ll discover that we’re calling e.currentTarget and e.preventDefault, which IE doesn’t implement. A really helpful function of pay attention is that, in IE, it’ll wrap func and fix this wrapper to the occasion. This wrapper calls func passing it a pretend Occasion object which is able to mimic these two options for IE.

Going wild with the DOM#section10

Including a listener to a component through id is okay when you might have only a few hyperlinks to pop up. If in case you have a lot of hyperlinks, it will get boring to code every listener one after the other. For that purpose, I current you with mlisten.

mlisten is identical as pay attention, nevertheless it takes a component checklist as a substitute of a component, and provides the listener to each factor on the checklist. The checklist could be a NodeList, or an Array of id strings and/or Factor objects.

Right here’s an instance of utilizing mlisten to pop up all hyperlinks in a listing:

  'click on',

In case your hyperlinks usually are not structurally grouped, you possibly can choose them by their class utilizing my operate getElementsByClass(className [, tagName [, parentNode]]):

  'click on',

The operate handles components with a number of space-separated courses nicely, so you possibly can safely use one class to fashion the factor and one other class to indicate habits.

Keep in mind, these calls must be made on the window’s onload occasion.

Through the use of listeners, we’re unable to supply parameters to the features. That retains us from passing in particular person window options to every hyperlink. Proper? Not fairly. Right here’s a fast resolution:

operate event_popup_features(options) {
  return operate(e) { 
    link_popup(e.currentTarget, options); 
}// onload…
pay attention(
  'click on',

There’s an necessary distinction to make right here. Say fn is a operate. While you write fn you might have a reference to fn. While you write fn() you’re calling fn and you’ve got no matter it returns.

Right here we’re utilizing a operate that returns a operate. We’re not assigning event_popup_features to the occasion, we’re calling it and assigning the operate that it returns. This operate is sort of a customized model of event_popup that takes into tài khoản the options we offered when calling event_popup_features.

That just about sums it up. Now that you recognize extra methods to pop up a hyperlink than you ever wished or wanted to, be happy to seize the supply code and the examples:

Leave a Comment