Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

arsenalgear

macrumors regular
Original poster
Dec 17, 2007
100
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! :)
 
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.
 
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:

<!ENTITY % XHTML.version
"-//yourname.com//DTD XHTML 1.1 plus Target 1.0//EN" >
<!ENTITY % xhtml11.mod
PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" >
%xhtml11.mod;
<!ENTITY % xhtml-target.mod
PUBLIC "-//W3C//ELEMENTS XHTML Target 1.0//EN"
"http://www.w3.org/TR/xhtml-modularization/DTD/xhtml-target-1.mod" >
%xhtml-target.mod;
<!-- End of XHTML 1.1 plus Target 1.0 DTD ......................... -->
<!-- ............................................................... -->
Your DOCTYPE would then become:
<!DOCTYPE html PUBLIC "-//yourname.com//dtd xhtml 1.1 plus target 1.0//EN" "http://yourname.com/dtd/xhtml11-target.dtd">
 
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.
 
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...
 
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.