View Full Version : Making links open in a new window
arsenalgear
Aug 9, 2009, 07:45 PM
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! :)
Cerebrus' Maw
Aug 9, 2009, 07:50 PM
You will have to use Javascript to make new windows open, if you are in Strict Mode.
There is a handy tutorial http://www.sitepoint.com/article/standards-compliant-world/, that tells you why target was removed, and how to overcome it.
angelwatt
Aug 9, 2009, 08:15 PM
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.
SrWebDeveloper
Aug 10, 2009, 01:22 PM
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">
Darth.Titan
Aug 10, 2009, 02:47 PM
To answer the original question, my preferred method is:
<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.
Eraserhead
Aug 10, 2009, 03:01 PM
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...
SrWebDeveloper
Aug 11, 2009, 09:00 AM
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
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.