Making links open in a new window

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

  1. arsenalgear macrumors member

    Joined:
    Dec 17, 2007
    #1
    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

    Joined:
    Mar 9, 2008
    Location:
    Brisbane, Australia
    #2
  3. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #3
    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

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #4
    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 "yourname.com" with your actual domain name:

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

    Darth.Titan

    Joined:
    Oct 31, 2007
    Location:
    Austin, TX
    #5
    To answer the original question, my preferred method is:
    HTML:
    <a href="destintion.html" onclick="window.open(this.href); 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

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #6
    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

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #7
    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.

    -jim
     

Share This Page