I'm using <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> (xhtml strict doctype) for a site I'm designing and when I validated it, I found that you cannot have target="" included else it's not valid.

It then went on to say that I should use CSS to do this.

I want to make one link open in a new window (have the same effect as target="_blank" ) using CSS.

Can anybody help?

Regards,
Martin

Dani AI

Generated

A short, practical addendum to the thread: is right that a Strict XHTML doctype will cause validators to reject the target attribute; was pointing at the hyperlink proposals, and ’s JavaScript workaround is the usual, pragmatic fix. Below are concise, modern recommendations (why, when, and a safe example).

XHTML 1.0 Strict / HTML4 Strict dropped the target attribute, so staying Strict means you should not put target in your markup; if you want a markup-only solution, use a Transitional doctype or move to HTML5 (which restored the attribute). (w3.org)

There is no reliable, cross‑browser CSS way to force a link to open in a new window. The CSS3 “target-*” properties were proposed, but they are not supported by major browsers, so you can’t rely on CSS alone. Use JavaScript progressive enhancement instead. (w3schools.com)

Unobtrusive JavaScript example (keep markup semantic, give links a class or data attribute, and open them on user click):

document.addEventListener('click', function (e) {
  const a = e.target.closest('a.external');
  if (!a) return;
  e.preventDefault();
  const w = window.open(a.href, '', 'noopener,noreferrer'); // popup opened only on user gesture
  if (w) try { w.focus(); } catch (err) {}
});

This uses the standard Window.open API and passes the modern security features (and falls back safely if blocked). (developer.mozilla.org)

Accessibility and security notes: avoid forcing new windows unless there’s a clear reason, and always warn users when a link will open a new window (put the note in link text or visually-hidden text). When a new tab/window is opened, prevent the opened page from accessing the opener (use rel=noopener / noreferrer or the window.open features above). (w3.org)

Summary: keep HTML semantic, enhance with JavaScript (not inline onclick), and add the small accessibility/security payoffs listed above — that’s what ’s workaround does in spirit, but using event listeners and rel=noopener makes it safer and cleaner.

Recommended Answers

All 4 Replies

this page would seem to say yes:

http://www.w3.org/TR/css3-hyperlinks/

bear in mind, most browsers have only got css2; and that specification seems to relate to css3.

you can do it with javascript if your stuck..

Thanks Matt,

I'll try it....

Regards,
Martin

I'm using <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> (xhtml strict doctype) for a site I'm designing and when I validated it, I found that you cannot have target="" included else it's not valid.

It then went on to say that I should use CSS to do this.

I want to make one link open in a new window (have the same effect as target="_blank" ) using CSS.

Can anybody help?

Regards,
Martin

In the place of target, put...onclick="target='newwindow'".
Then it will pass strict validation.
Sincerely,
Dynamictc

In the place of target, put...onclick="target='newwindow'".
Then it will pass strict validation.
Sincerely,
Dynamictc

Thanks Dynamictc,

I tried that and it works great.

Thanks again,
Martin

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.