Code question

How do I modify <li><a href=“rules2012.pdf”>Rules & Regs</a></li>
to have it open in a new window?

Hi,

You could use the target attribute, e.g. <li><a href="rules2012.pdf" target="_blank">Rules &amp; Regs</a></li>

However, this has been deprecated as of XHTML 1.0 Strict and XHTML 1.1. Plenty of people will also tell you shouldn’t have links open new windows because it’s insulting to your users (they’ll open a new window if they want to) and it breaks the back button because the new tab/window doesn’t retain your browsing history.

I’m not really sure where I stand on this, but here’s an interesting article on the subject if you wish to read more: http://jalada.co.uk/2011/02/10/why-you-shouldnt-use-target_blank-and-what-to-use-instead.html

I’d also be interested to hear other people’s thoughts on this.

No, not really ‘deprecated’ it [_blank] was meant for use alongside with 4.01, Frameset only. XHTML 1.1 is modular and it has never appeared in any Strict Doctype: http://www.w3.org/MarkUp/2004/xhtml-faq#target for obvious reasons that it is the realm of behavioural unobtrusive JavaScript.

Recently we’ve had a discussion of “Separation of Concerns”, as to why _blank should not be misused: #29 and [URL=“http://www.sitepoint.com/forums/showthread.php?895775-Caption-not-going-where-I-want-it&p=5219880&viewfull=1#post5219880”]#39 and why you should always inform the user of popup link functionally.

In the case of PDFs, as in the example, different browsers/systems behave in different ways by default. On my (Linux) system, PDFs open in the document viewer, not in the browser.

Precisely Bear, like I briefly mentioned in the other thread, in either-case the link could theoretically benefit from the PDF MIME hyperlink type=“application/pdf” attribute for the content.

Thanks for the link to the threads concerning “_blank” xhtmlcoder.
I’ve just finished reading them and that actually really helps me to clarify my opinion.
What I’d been doing so far was using “_blank” and displaying a small symbol informing the user that the link would open in another window.
I guess I’ll rethink that now.

Pullo, glad you’ve seen the light. At least previously you’ve considered informing the user the page will possibly open in a new window. :slight_smile:

You could use jQuery. For example, give each PDF link a rel of “pdf”:

<a href="rules2012.pdf" rel="pdf">Rules &amp; Regs</a>

… and then use some jQuery like this:

$('a[rel="pdf"]')
.click( function() {
window.open( $(this).attr('href') );
return false;
});

So a full example page would look like this:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>

<p><a href="rules2012.pdf" rel="pdf">Rules &amp; Regs</a></p>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script>
  $('a[rel="pdf"]')
  .click( function() {
  window.open( $(this).attr('href') );
  return false;
});
</script>
</body>
</html>

I just use target blank. I see no reason to use js for this when target does it better. As a general rule I only open off site links in new windows if I do it. Or just recently I added a FAQs page. I have 50 click to open facts and each has an included link to more info. I used target blank there too even though they are inter site links. There is something to be said for keeping the user on your site. And not purposefully launching them off just because other say its better.

A recent search for a similar problem revealed this elegant inline solution:



  &lt;a href="rules2012.pdf" onclick="window.open(this.href); return false;"&gt;
      Rules & Regs
  &lt;/a&gt;


I shall reiterate: _blank is for use with Frameset (DUAM XNAHT). The complete misuse of _blank is not guaranteed to keep people on your site and if anything it is more likely to distract them and make them wander off somewhere else.

The age-old generic argument “I don’t want people to leave my website!” (so I’ll force a popup) was the kind of craziness you saw least century… I first mistook thee for a statue, when I regarded thy head of stone.

[ot]Note: if a user wants to leave, they’re going to leave. If you want them to stay, make them want to stay. Anyone who thinks they need to force their visitors to stay on their site should focus on writing more interesting copy rather than spawning windows.

To put it another way: if you think FONT and CENTER and U elements are COOL, then feel free to use target as well - if not, put it in an external, unobtrusive JavaScript file.[/ot]

The new window thing becomes moot as more and more browsers open those links as new tabs by default anyways.

I learned through user testing to open PDFs in new windows though. All the users we tested were generic Windows users who used IE. If we didn’t abuse target or use JS to open the PDF in a new window, the users always closed the whole browser while assuming they were only closing the PDF. Every time. Luckily our test browsers remembered the last-opened windows…

I find the idea of using outdated Frameset attributes to open new windows quite funny, because we’re not using _top or any of the other frame names (though I’ve seen _self on websites still! Amazing).

It’s just retardation and finding out 2 hours later that you’ve still got a window open of a site you left 2 hours ago means, as Crusty would say, 2 things, and Jack left town.

As a general rule I only open off site links in new windows if I do it. Or just recently I added a FAQs page. I have 50 click to open facts and each has an included link to more info. I used target blank there too even though they are inter site links.

Thank the gawds my browser can override whatever some webbie writes. I can’t think of one good reason why I would want a separate window for ANYTHING. Well, unless I was using IE6 which doesn’t have tabs. New windows had more reason to exist with IE6 around.

I don’t hate that idea as much as I used to, but if there were more than say, 3 links on the page I was doing that to, I’d still go back to an external JS file who would find all the links for me and grab their href values and whatnot. Cuz I’m lazy.

Whenever you can avoid using js do so.

As a general rule I only open off site links in new windows if I do it. Or just recently I added a FAQs page. I have 50 click to open facts and each has an included link to more info. I used target blank there too even though they are inter site links.

this makes perfect sense; a good example would be a web designer portfolio page where you click on an example and it sends you to the actual live site. If the user starts browsing that site… it might be a mess of back buttons for him/her to navigate back to the porftolio. On the other hand if you are doing some 1998 crap where you open images in pop up window just to have them display bigger … ugh… dont! you can easily use CSS to modal screen (if you dont feel like doing it yourself there are may prewritten scripts out there)

So as with most question the answer is “it depends” on the specific specif situation.

Mercy! Dost thou wish me to die of laughter? I do hope it wasn’t 50 new; _blank forced spawned windows/new tabs from one (none framed) page? Pretty inefficient and bloated to say the least… Obviously that is a clear case of need for unobtrusive JS if there ever was for handling window management. I despair where are people learning to write markup nowadays, surely they don’t advocate such crazy practice in HTML 4.01 Reference books or learning materials?

Thank you yes I agree. There is a time and a place for. Another good example is Gmail. Gmail uses target blank. Just like my extra info 50 links, on Gmail you dont want to leave the page, you just want to see the link, click around a bit, and then come back. It works in my case. And many others. JS slows down sites. Target blank does not.

When I open Sitepoint Forums I click on the “Whats New” button. Clicking on any thread opens the thread, I prefer opening the thread in a new window…

…which is why browsers allow you, the user, to choose. And as I said, luckily the browser allows us to override someone else’s decision they wanted to make for us.

Ain’t nothing stopping anyone from right-clicking (or CTRL-ENTERing) any link and forcing it to a new tab or whatever all by themselves.

I must be becoming a libertarian or something o_O

The thing is, as often is, with people smarter than average, you forget that 90% of the people out there would forget how to wipe their ass if they didn’t get a constant reminder each day, much less remember how to work a computer. I am in the unique position of being of average intelligence, which gives me a clearer view of the average user. And the average user is who I code my site for. Not the uber-nerds.

Edit. However on principal I agree with you. I don’t like to be told what to do either.

I don’t think anyone is arguing that in some instances a new window would be good; it’s more the case of how the author writes the method or (informs or doesn’t) the user that the link has been designed to open in such a way. That is the major point repeatability being made.

A truly idiotic solution by the forum software in action.

In a secured control panel session ‘user login’ type environment then it theoretically could make sense for the prevision for unobtrusive JS to open some control toolbox windows. Certainly if I had to suffer five or more new windows forced by _blank it would probably overload my memory, I have of course a disability and it’d probably result in my closing them all.

The JS processing cycles to open a new window would be negligible in this instance. The actual opening of the new window/tab by any method is what would consume resources and when you abuse target and let’s say to have 3 windows open that’s 2 (orphans) you’d need to close. Whereas with in contrast unobtrusive JS you could keep it manageable or even in absence of JS the same applies - easy management allowing the user to deicide.

Poes, is an example of a dumb user so if a stupid cat wants choice; then it’s clear unobtrusive JS is the right choice, as far as the visitor is concerned.

I agree with poes that the fact that many browsers open a new window in a new tab makes this less of an issue than it once was; and I also click command/control to open links in new tabs … but most people probably don’t know about that option. At the end of the day, target was deprecated for a reason, but I guess it was also re-introduced in that bad-boy HTML5 for a reason, too … :rolleyes:

I still see my mother get confused when a page opens in a new tab (I’ve seen her fumbling for the back button and not sure why it’s not working … and then closing the browser altogether …) so I’m still not keen on the target attribute. I’ve tried to teach her how tabs work, but it’s a slog.