views:

1108

answers:

6

I've curious how this should be properly done.

I ran a page im working on through the w3 html validateor and I got one error message

Line 47, Column 54: Attribute "target" exists, but can not be used for this element.**

<ul><li><a href="./jobops/1000 Design PM.pdf" target="blank">1000 Design PM</a></li>

You have used the attribute named above in your document, but the document type you are using does not support that attribute for this element. This error is often caused by incorrect use of the "Strict" document type with a document that uses frames (e.g. you must use the "Transitional" document type to get the "target" attribute), or by using vendor proprietary extensions such as "marginheight" (this is usually fixed by using CSS to achieve the desired effect instead).

any idea on how i can have a link open a new window but not use the target tag?

+1  A: 

Shouldn't it be...

target="_blank"

Regardless... You could open a new window using javascript, but that breaks the beauty of simple browsing. What if I'm browsing using Lynx or something?

jjclarkson
It should be, but that doesn't address the problem
David Dorward
window.open is a function. You call it with arguments, you don't assign URLs to it.
David Dorward
Don’t abuse `rel` for that. Just put those links in a class named `_blank` or so.
Gumbo
There, fixed that.
jjclarkson
+2  A: 

You can use JavaScript to open new windows, which avoids the issue of target being invalid in modern HTML.

However, this bypasses various systems people have in place to warn them about new windows (or prevent them from opening) so you are better off using the target attribute (and switching to a Doctype that allows it).

Better still is to leave it to the user to decide when they want a new window. Aside from the annoyance factor, they do introduce accessibility problems.

David Dorward
+1 for "Better still is to leave it to the user to decide when they want a new window"
Mayo
A: 

Using:

target="_blank"

breaks strict XHTML validation methods. Here's a document detailing a workaround:

Dominic Bou-Samra
A: 

Instead of target="_blank" (who will never validate) you could use javascript like this:

<a onclick='window.open("./jobops/1000 Design PM.pdf", "_blank");return false;' href="./jobops/1000 Design PM.pdf">1000 Design PM</a>

Then the link will open in a new window, and your page will validate.

The users who doesn't have Javascript enabled (even though that is only about 2% of all users), will still be able to follow the link with this method. The guy's have a good point in the comments :)

Have a nice weekend everyone...

Kim Andersen
Is that a safe method though - I know some people run without Javascript enabled (not often admittedly). Or does that just apply to defined scripts?
Dominic Bou-Samra
There are lots of ways you could implement this using window.open, and this is the worst. If JavaScript is not available, it links to the top of the page. For that matter, since there is no return value from the event handler, it will always link to the top of the page (even if the new window is opened).
David Dorward
Really it should be `<a onclick="window.open("./jobops/1000 Design PM.pdf", "_blank");return false;" href="./jobops/1000 Design PM.pdf">1000 Design PM</a>` to allow users with javascript disabled to download/open the pdf.
voyager
Better: `<a onclick="return !window.open(this.href, '_blank')" href="./jobops/1000 Design PM.pdf">`.
Gumbo
onclick="return !window.open(this.href, '_black');"… is neater. But window.open is still the worst option out there.
David Dorward
And he did say "COULD" be used. It's a valid method - not particularly recommended - but still valid.
Dominic Bou-Samra
I see Gumbo and I have been reading the same patterns :) (Is this a good time to mention that inline event handler attributes are dirty?)
David Dorward
Of course, if he has a js framework, he could simply add a class to all links that should open in a new window and add the window.open to them with their link, but that is besides the point. This solution *is* valid.
voyager
Why was i downvoted at this one? Crash893 asked about how he could open a link in new window without using the target attribute, and this is one way of doing it.
Kim Andersen
you can't ensure that an xhtml1.1 strict browser has any knowledge of the 'target' attribute.
Evan Carroll
+1  A: 

The target attribute is not part of the Strict variants of HTML 4 and XHTML 1.0 as well as XHTML 1.1.

So you would need to use a workaround using JavaScript:

<a href="./jobops/1000 Design PM.pdf" class="_blank">1000 Design PM</a>

var aElems = document.getElementsByTagName("a");
for (var i=0, n=aElems.length; i<n; ++i) {
    if (/(?:^|\s+)_blank(?:\s+|$)/.test(aElems[i].className)) {
        aElems[i].onclick = function() {
            return !window.open(this.href, "_blank");
        }
    }
}

Or (in the future) CSS 3 (see Hyperlink Presentation Module):

a._blank {
    target: new;
}
Gumbo
Target isn't part of HTML 5? http://www.w3.org/TR/html5/history.html#attr-hyperlink-target
David Dorward
@David Dorward: Fixed it ;)
Gumbo
This isn't really a work around for anything but a validator. You're still just tacking on an attribute not in the spec you're stating the document is apart of, and hoping that the browser has implimented this external behavoir of `target`. Instead, you need to add an event on click to open up the link in a window.open. See http://www.badlydrawntoy.com/2009/03/03/replacing-target_blank-for-strict-xhtml-using-jquery-redux/ for more info.
Evan Carroll
+1  A: 
target="_blank"

Will not validate strict because the 'target' attribute has been deprecated.

Instead, try something similar to the aforementioned onclick workaround, but you don't need the "_blank" in there either. Simply:

<a href="./jobops/1000 Design PM.pdf" onclick="window.open(this.href); return false;">1000 Design PM</a>

Will work. The reason for the deprecation of "target" is because HTML is used to semantically mark up data whereas the target attribute was providing behaviour, which is what javascript is for.

If the user has javascript turned off then the URL will simply open in the same window.

Evernoob
target is not deprecated, it just doesn't appear in Strict variants.
David Dorward
Onclick throws a violation as well
Crash893
What is the violation?
Evernoob
same as target. It just says on click is not part of the strict html law
Crash893