I have the following piece of HTML that creates a new window when clicked:
<a href="/path/to/site" target="_blank">glide by the people</a>
Problem is that it does not pass the W3C validator. How do I create a link like the above that does validate?
Assuming strict XHTML, you would bind an onclick event to the anchor in question. Something like:
One would also argue that you should be binding that onclick action separately with external JavaScript due to progressive enhancement and separating behavior from your markup, but that's the basic way to go about it.
Validation isn't the be all and end all of coding quality.
Some things are "standard" in browsers without being a w3c standard.
Using a bit of JS is a little overkill when the function already exists.
rel="new-window"
attribute to the link (instead oftarget="_blank"
)add jquery script to head of page and add the following snippet
(Please note that as I typed this in the stackoverflow textbox, I haven't tested it.)
Actually, the proposed here solutions with adding the "target" attribute through JavaScript are completely standards compliant!
The thing is that according to the W3C DOM Level 1 specification the interface HTMLLinkElement DO have target attribute, although the A element from HTML 4.01 specification do not have it.
So it's invalid to write "target" attribute in html file, but is valid to add it to the document tree later via DOM interfaces using JS.
Use this doctype:
1.0 transitional accommodates some html "legacy" code, including target="_blank".