Making links open in a new window

Discussion in 'Web Design and Development' started by arsenalgear, Aug 9, 2009.

  1. arsenalgear macrumors member

    Dec 17, 2007
    Hi there,

    With the <a> tag's "target" attribute deprecated in the Strict DTDs for HTML and XHTML, how do you make links open in a new window when writing code that conforms to them?

    Thanks! :)
  2. Cerebrus' Maw macrumors 6502

    Mar 9, 2008
    Brisbane, Australia
  3. angelwatt Moderator emeritus


    Aug 16, 2005
    I'm not completely in agreement with its deprecation, but I do see the rationale from separation of content and behavior (and presentation). JavaScript can give the functionality back, but unfortunately, there's not much in the way graceful degradation for user's with JavaScript disabled for whatever reason. Personally, I just use a middle click on my mouse to open links in new tabs. I tend to even do it on Mac Rumors, even though posted links are set to open in a windows.

    I also have to wonder if it will ever truly go away. I mean the i and b tags were also deprecated, but HTML5 has brought them back. They simply modified their definition. I doubt I'll use the i and em tags ever again though.

    Finally, validation should be thought of more as a guideline. If you understand why something doesn't validate and you're OK with it, then power to you. I mean just look at the validation of the Google home page. It's horrid, but they have their reasons, and I feel they're legit reasons.
  4. SrWebDeveloper macrumors 68000


    Dec 7, 2007
    Alexandria, VA, USA
    Furtunately XHTML is XML, and in XHTML 1.1 you can create your own DTD which means you can "add" in target support as a module to extend the default DTD.

    Create a dir called dtd off your document root, save using filename "xhtml11-target.dtd" and in all examples below replace "" with your actual domain name:

    Your DOCTYPE would then become:
  5. Darth.Titan macrumors 68030


    Oct 31, 2007
    Austin, TX
    To answer the original question, my preferred method is:
    <a href="destintion.html" onclick="; return false;">Link</a>
    For people with javascript enabled you get a new window. For users with javascript disabled at least the link still works.
  6. Eraserhead macrumors G4


    Nov 3, 2005
    Can't you just change the doctype to transitional? Forcing people to use Javascript to open a link when there is a method that works in all browsers without it seems a bit crazy...
  7. SrWebDeveloper macrumors 68000


    Dec 7, 2007
    Alexandria, VA, USA
    Using the method I stated solves all issues with JS, maintains strict mode which is a good thing and the site will validate, too, since the definition for the target argument is right on your own server. Hence the suggestion.


Share This Page