Opening links in new window: the XHTML way

Roger Johansson said the following about using the target attribute:

The target attribute is not allowed in strict doctypes, and since you should always use a strict doctype, the target attribute is invalid.

It’s possible to use a JavaScript trick to open a link in a new window and still having a valid XHTML Strict syntax.

To do so, add this attribute to your links rel="external". Then, use the following script:

window.onload = function() {
  var links = document.links;
  for (var i = 0, s = links.length; i < s; i++) {
    if (links[i].rel = 'external') {
      links[i].target = '_blank';
    }
  }
};

With jQuery you can use a smaller code:

$(function() {
  $('a[rel=external]').attr('target', '_blank');
});
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s