New windows with JavaScript and the target attribute demo page

This is a demo document for the technique described in the article New windows with JavaScript and the target attribute. Please refer to the article for details and context.

The following link has a new-window class name and will open in a new window or tab depending on browser settings when JavaScript is available: 456 Berea Street.

If your browser is configured to open new windows in tabs (like Firefox is by default) or the same window, this link should not interfere with that. Neither should it interfere with your browser's ability to inform you that the link will open in a new window (which Safari does).

The script is available in jstarget.js, which has no library dependencies. If you’re already using jQuery, you could achieve the same effect with something like this:

  1. $(document).ready(function () {
  2. $("a.new-window").append(' <em>(new window)</em>').attr('target', '_blank');
  3. });

Lab Index | 456 Berea Street Home | Copyright © Roger Johansson