tags:

views:

852

answers:

10

The title says it all. Does anyone have a sollution?

+4  A: 

Use this doctype:

<!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhmtl1/DTD/xhmtl1-transitional.dtd"&gt; 

1.0 transitional accommodates some html "legacy" code, including target="_blank".

ghoppe
Is there any way to get the same functionality w/out resorting to the transitional doctype?
Nate Bross
Not without using javascript. (see the post by @ajm below) Reason why this was removed was due to accessibility and user interaction. Users want control over that stuff, not popups. :)
ghoppe
+3  A: 

Assuming strict XHTML, you would bind an onclick event to the anchor in question. Something like:

<a href="/path/to/my/link" onclick="window.open('/path/to/my/link');return false;">My link</a>

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.

ajm
Minor gripe: DRY! replace the URL in the onclick event handler with `this.href`. Also, move that whole thing into a JavaScript file instead of cluttering up your HTML.
Alan
+1  A: 

You can not do it with W3C strict validation.

The solutions are:

    $(document).ready(function(){
          $('A[rel="_blank"]').each(function() {
               $(this).attr('target', '_blank');
          });
     });

     $(document).ready(function(){
          $('A[rel="_blank"]').click(function() {
               window.open($(this).attr('href'));
               return >false;
          });
     });
  • Also, as per this doctype page, HTML5 should allow target atrtribute to be validated, fow what it's worth.
DVK
+2  A: 

You can do this cleanly with some JavaScript if you want to be Strict compliant, the convention seems to be rel="external".

If jQuery is an option:

<script type="text/javascript">
  $(function(){
    $('a[rel="external"]').attr('target','_blank');
  });
</script>
<a rel="external" href="http://www.google.com"&gt;Google in new Window</a>
Nick Craver
How is this different to just having the target blank attribute on the link - hiding things from a validator doesn't make it valid!
Rich Bradshaw
@Rich - There are lots if invalid things javascript does that XHTML doesn't like. Example, take just about *any* page that does more than a little javascript, e.g. google.com, validate it, select all in firefox to get what the current markup would use, view souce, copy and validate that markup...in **most** cases you'll have more validation errors.
Nick Craver
Why not just put your entire page inside a single document.write() call? That would validate 100% of the time, right? I agree with other posters that adding javascript for the sole purpose of making your page pass the validator is silly.
davr
@Rich - hiding things from validator does not necessarily make things better but it makes things pass the validator and thus avoid red tape.
Lemurik
@davr I don't disagree but like Lemurik if you need functionality and need to pass w3c validation (when the validator requirement is beyond your control), you have to have an option. Unfortunately, not all decisions in programming are based on intelligent engineering or common sense, sometimes they're made well over the programmer's pay grade.
Nick Craver
@Nick That's all fine and dandy but sometimes these validation things are in the spec to encourage programmers to think about whether they really *need* the functionality. Why would you *need* a link to open in a new window?
ghoppe
@ghoppe - Any RSS style feed I suppose...really anything where you don't want to navigate away from the min site I suppose. I agree, I haven't used this in years, but I'm sure there's plenty of legitimate uses out there.
Nick Craver
In the `<a>` tag, you say **ref** = "external", but in the jQuery you search for **rel** = "external". Is this right?
Mike DeSimone
@Nick At the risk of being argumentative, an RSS feed is precisely what I _do not want_ opening a new window. I use an external feed reader -- having a pop window would be annoying. I can't think of a single use that couldn't be better handled by a modal hidden javascript div. Leave window management in the user's hands, I say.
ghoppe
@Mike - good catch, fixed!
Nick Craver
@ghoppe - Ok so bad example :) My point was simply there are valid uses out there is all. If there were better options to let the user manage it (other than right, middle or ctrl+click which most don't know...) then I'd agree. In my experience though, the majority of users don't know how to do this, and it's unfortunate.
Nick Craver
Why would you need to pass validation - I still say that writing invalid code using a different language just to fool one specific website at the detriment of some users is just silly.
Rich Bradshaw
@Rich - As a programmer, I wouldn't, and I **completely** agree with you, but as an employee who works for a manager who's directed somewhat by a marketing department that wants to say we're 100% XHTML valid, I might need to...and yes, I've been in that situation unfortunately. Some arguments you just can't win with reason, but that's how the corporate world works at times.
Nick Craver
For some reason your sollution doesn't work for me, I fixed the <scrip*t tag, are there any more errors in the code?
Chris
@Chris - There shouldn't be, didn't notice that `t` but fixed in the answers now. I just tested here in Firefox/Chrome/IE...are you using jQuery at all? Unless you are using/including it, this solution won't work for you.
Nick Craver
@Nick That scares me somewhat. Perhaps the validator should be updated to parse and execute any js before validating... That would mix things up a little wouldn't it!
Rich Bradshaw
@Rich - I'd **love** for that to be the case...from what I've seen, it'd solve a lot of SO questions just by the validator showing the user markup mistakes. I'm all for validation, but I'm with you, validating for the sake of validating is absolutely fine, but *changing* just for the sake of validation in silly in a lot of cases such as this.
Nick Craver
@ Nick, I don't use jquery at all. But now I know why ;-)
Chris
+3  A: 
  1. Validation isn't the be all and end all of coding quality.

  2. Some things are "standard" in browsers without being a w3c standard.

  3. Using a bit of JS is a little overkill when the function already exists.

Rich Bradshaw
What if W3C validation is a coding standard requirement?
Lemurik
Indeed. Validation is a great tool, but it's not like W3C are gods that get everything right. The standards aren't perfect
PatrikAkerstrand
@Lemurik: Every browser understands and accepts target="_blank". Just because it's not in the W3C standard doesn't make it incorrect.
PatrikAkerstrand
All are good points, but on the other hand standards are standards for a reason. There are reasons of usability and accessibility not to use target="_blank". (It breaks the back button, some user agents, etc.) Forcing new windows to open is a bad idea.
ghoppe
@Machine It doesn't mean that it isn't still a bad idea.
ghoppe
@Machine - you are missing my point. The user may have a perfectly legit need to comply with W3C validator whatever his, your or mine opinion on the accuracy, usefulness and goodness of said validator is.
Lemurik
I outright challenge this particular validation as bogus.
Joshua
@Joshua. Bring it up with the W3C then. :) I take the contrary view. Window management should be under the control of the user. What about the visibility impaired? Don't break my back button, bro.
ghoppe
@ghoppe @Lemurik: The W3C standard also says that the <script> element must have an type-attribute. It isn't, however, up to the client to decide the type when requesting a resource. The server responds with a type. This attribute is IGNORED by the server. It was put into the HTML-spec since the commitee sought to replace JavaScript in the future. My point is, just because something is in the standard doesn't mean that it is neccessary, or even correct. A lot of interests are involved when they are written.
PatrikAkerstrand
@ghoppe I agree with you that not using _blank has some real benefits. I also don't like when this decision is taken for me. But should it pass validation? Yes.
PatrikAkerstrand
@Machine If you don't like it if the decision is taken for you, and the W3C has agreed with you that this is a bad practice and should be discouraged in the future, then why do you think _it should pass validation_ with the updated spec? Past bad decisions should not be written in stone.
ghoppe
@ghoppe since the response to failing _blank (look at this thread it's enough proof) is all too often writing a link that doesn't even work w/o javascript I'd say that we're better off if it does pass.
Joshua
+1  A: 

If it's just one or two links, you can do it inline with

<a href="http://stackoverflow.com" onclick="window.open(this.href); return false;"></a>

More than that and you probably want one of the js solutions above.

Tom
I like this one. thanks!
Chris
+1  A: 

I think you're asking the wrong question. The target attribute is not valid in strict XHTML 1.0 no matter whether you insert it with JavaScript or just have it in the server response.

If you really want that attribute, you have to use a different doctype, but that's not really the right answer either.

You should ask yourself why you want the attribute. I'm guessing you're trying to create a new tab or window. Needless to say this is generally considered bad design (it takes control away from the user), but if you really want to do it, you could do it with JavaScript.

Here's how:

Keep your links but add a special class, e.g. "popup" to them. Then add a line of JavaScript (preferably using a framework like jQuery or Prototype to make it easier) that takes all links with that class and gives them an on-click handler that results in the creation of a new tab/window and cancels the default action (i.e. stops the link from working as a link). That will still annoy people as it overrides the expected behaviour, though.

What you should not do is replace the links with dummy links and rely on JavaScript for the links to work.

Alan
I don't have a lot of external links, just one in fact. My document was completely validated accept for the new window part. I have seen a lot sollutions here. thanks for your reply
Chris
+2  A: 
  1. Add rel="new-window" attribute to the link (instead of target="_blank")
  2. add jquery script to head of page and add the following snippet

    <script type="text/javascript">
        $(document).ready(function(){
            $('a[rel="new-window"]').click(function(){
                window.open(this.href);
            })
        });
    </script>
    

(Please note that as I typed this in the stackoverflow textbox, I haven't tested it.)

Kris
A: 

Here is the solution through Jquery

http://www.ajaxblender.com/open-links-new-window-w3c-valid-target-blank.html

Paniyar
A: 

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.

NovA