If you set the target attribute on an a-element the code won't validate at http://validator.w3.org how does one come around this and still have it validating with the targetattribute that is?

4 answers

This was chosen as the best answer

That the page don't validate isn't that big of a deal. But what it says is more important. It probably says that the target-attribute is deprecated in that particual standard, which is W3 parlance for don't use this old junk anymore.

An important note is also that you should try to let the users decide what they wan't to do with their links, open it in a new tab, window or whatever.

If you absolutely need to open some links in a new window, use JavaScript. You can use a global script like this:

document.onclick = function(e)
  var target = e ? e.target : window.event.srcElement;    

 while (target && !/^(a|body)$/i.test(target.nodeName))
    target = target.parentNode;

  if (target && target.getAttribute('rel') && target.rel == 'external')
    var external = window.open(target.href);

    return external.closed;

The above script detects if someone clicks on a link that has an rel-attribute of "external".

This way, you can put the attribute rel with an value of external like this <a href="#" rel="external">External link</a> and it will automatically open in a new window if the user has JavaScript support.

Answered over 9 years ago by Jens Hedqvist
  • I would argue strongly against mucking up your code to please a validator when all modern browsers fully support the target attribute... I'd recommend you start using HTML 5, and use some sort of plugin (like modernizr, http://www.modernizr.com/) to deal with browsers that don't support it. Christopher McCulloh over 9 years ago
  • Yepp, I agree. It's not about pleasing the validator in this case. I think it's about semantics. I don't know if I like that you should specify behaviors with a HTML target attribute on links. Jens Hedqvist over 9 years ago
  • True, better to let the user "right" click and select what they want to happen from the menu... I think both the JS and HTML target way place an unnecessary control on the user... But I guess I'd rather the two word HTML version than try to maintain the 7 lines of JS... However, the JS validates, so it's a trade-off Christopher McCulloh over 9 years ago
  • Ok great thanks, not everyting is up front how it "should" work on the web I guess, but I agree behaviour that should go with JavaScript... Chris over 9 years ago
  • The seven lines of JS-code is site-wide so it's a global solution for any link on the site. But yeah yeah I rest my case, I won't pursue an optimization debate. It's Friday ffs :) Jens Hedqvist over 9 years ago

Change your doctype to XHTML transitional instead of strict.

Something like this:

<!DOCTYPE html 
 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
Answered over 9 years ago by Rob Crowther
  • That is also an solution if you need the target attribute. :) Jens Hedqvist over 9 years ago
  • Or try XHTML 1.1 strict, I believe it supports target... (not 100% sure) Christopher McCulloh over 9 years ago
  • I don't think any of the strict variants support it, that's what the FAQ I linked to in the first line is discussing. Rob Crowther over 9 years ago

The HTML 5 spec allows for <a target="whatever">link</a>. Check out the Modernizr plugin (from Paul Irish) to bring old crappy browsers into the future.

<a href="#blah" target="_blank">Is valid</a>

See Example

Answered over 9 years ago by Christopher McCulloh
  • That's because you are not using a valid doctype, so the validator defaults to HTML 4.01 Transitional where the target attribute is allowed. :) Jens Hedqvist over 9 years ago
  • http://www.w3.org/TR/html5/text-level-semantics.html#the-a-element Christopher McCulloh over 9 years ago
  • <!DOCTYPE html> <-- The HTML 5 doctype, which is specified at the top of the code example I linked. Christopher McCulloh over 9 years ago
  • HTML 5 are for sissies ;P (mainly because I haven't got around to use as much as I wan't ;) ) Jens Hedqvist over 9 years ago
  • lol. HTML 5 rocks. Check it out: http://codinginparadise.org/presentations/intro_html5.pdf Christopher McCulloh over 9 years ago
  • Great resource! Thanks! I will study until my scrawny coding eyes hurt. Jens Hedqvist over 9 years ago

You as web developers over-estimate many users if you depend on the browser's features. Some users have no clue what a browser is much less their features.

That is why you see the question of the valid target attribute come up all the time.

Answered almost 10 years ago by altoman