Target=“_blank” vs window.open

I’m working on an application where users enter some input and they shouldn’t leave the page when clicking on share links, e.g. a Facebook share link:

<a href="https://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.example.com%2F">Share on Facebook</a>

I know it’s very common to use popup windows for such links. But does it have any advantage over a simple target=“_blank” attribute? How do you compare the two methods and which is the right practice in such cases?

Hello Rain,
I personally feel target=“_blank” is better for experienced user. If your application is for all common people then I think “popup” is best. I am not positive with window.open. :frowning: But for security reason it may be good. Fell free to share your thought.

Personally I think that pop-ups must be used with moderation and just to display or request for some information.

Consider the fact that a number of users configure their browsers to block pop-ups, and some browser automatically block pop-up windows by default, so you would have a problem.

Either one just removes one of the options that your visitors have on where they can open it if they don’t know how to override your code.

I have my browser set to strip all the antiquated target attributes out of the page and redefines window.open(x) as location.href=x;.

To me it depends on the reason. eg.
To keep visitors on my site in case they might not come back - wrong reason, if they want to leave some will be annoyed and they’ll just close the site’s tab anyway if they’re done with it.
To provide information that might be needed to understand the page they’re on (though tabbing back and forth might be annoying).

IMHO it’s best to identify such a link with some sort of clue. eg. “external site opens in new window” or type of thing

I know it’s very common to use popup windows for such links. But does it have any advantage over a simple target=“_blank” attribute?

I can see this being an instance where the user wants to share but doesn’t intend to actually visit Facebook etc. Popups are popular but because of popup blockers, they’re often not really popups (not whole new windows). Instead, they’re little divs that appear and sit over the page near where the user clicked and load the social media stuff via Ajax (using the social media site’s API to deal with cross-domain).

If this isn’t a possibility for you for whatever reason and you don’t want to use one of those plugins (shareThis, addThis) then between the two I’d use target=blank. Why? Users can configure if that actually opens a new copy of the browser or just a tab, and like Felgall can override either so right-click works (most users, being trained on the social media plugins to expect a small popup, will not think to try a right click though). Many browsers default to a new tab (though not IE it seems). Users are nowadays pretty familiar with switching between tabs. The main issue with this is, will the user always know a new [tab, window] opened? Also navigating to and from either a new window or a new tab is a break from whatever the user was doing on the original page, which is bad.

But I’m assuming you are choosing between two bad options :slight_smile:

In general, yes, though this is a special case. People have had the experience of social media “Like” and “Share” and “Tweet this” buttons already shoved down their throats. Here, they already have certain expectations, so the OP should try to fulfill them as much as possible.

I tried the default setting of the latest version of all major browsers: they all open a new tab.

The main issue with this is, will the user always know a new [tab, window] opened?

Yes. Because as soon as the new tab opens, the user is automatically taken there, and when the Share/Cancel button is pressed the tab automatically closes and you get back to the main page.

Also navigating to and from either a new window or a new tab is a break from whatever the user was doing on the original page, which is bad.

Common problem between a popup window and a new tab.

If this isn't a possibility for you for whatever reason and you don't want to use one of those plugins (shareThis, addThis) then between the two I'd use target=blank.

Agreed! Thanks for the answer! :slight_smile:

P.S. MDN discourages using popup windows for some good reasons.

I’m not sure you can rely on that happening; my browser preferences are set to ensure it doesn’t.

And Opera settings allow forcing new window instead of tab

And IE, like Firefox, also allows for newly opened tabs to not automatically become active

Safari allows setting both no tabs and not automatically active

Yes. Because as soon as the new tab opens, the user is automatically taken there, and when the Share/Cancel button is pressed the tab automatically closes and you get back to the main page.

Unfortunately (for you) you can’t rely on that behaviour, probably because for enough users it’s disorienting (well, so is opening a new window too). My tabs open to the right of the current tab or at the end (far right) of the tab list, depending on my browser. I tend to see it out of the corner of my eye, but there are times where I’ve missed it. This is what you may have to worry about.

I tried the default setting of the latest version of all major browsers: they all open a new tab.

I have a copy of IE8 on my work machine (for testing at work) and it opens new windows, but maybe MS changed this since 8? If so, that’s great.

P.S. MDN discourages using popup windows for some good reasons.

Yeah, there’s a lot of good reasons indeed.

Again, what those share plugins usually use isn’t a window.open popup (the kind that get blocked), but just an ajaxy div. That way they get around popup blockers, plus the user’s focus usually gets to stay on the button/link they clicked (depending on how they coded it).

I’m talking about the default/usual setting in users’ browsers. If we worry about changing the default setting, then we should stop using the target attribute forever and W3C should deprecate it. Or coders should stop using JavaScript altogether because users might disable it in their browsers’ settings.

We’re not saying that you shouldn’t use things, simply that you shouldn’t rely on them working as you want at the client end.

No, coders shouldn’t “stop using JavaScript altogether because users might disable it in their browsers’ settings”, but they should allow for that possibility and ensure the site functions without JS. I keep JS disabled by default, and if I encounter a site which doesn’t work without it, then I simply ignore it and move on, unless I have no alternative but to use that site.

(Simply adding a message saying “You must enable JavaScript to use this site” is not an adequate way to deal with the issue, despite the ever-growing number of sites/designers who seem to think it is. :rolleyes:)

… we should stop using the target attribute forever

Many have said so, [url=http://www.smashingmagazine.com/2008/07/01/should-links-open-in-new-windows/]for various reasons.

However, some people have stated that [url=http://css-tricks.com/use-target_blank/]there can be a small number of good reasons and so we will be very, very careful web developers when we use it.

and W3C should deprecate it

They did. Then they un-deprecated it. This is partially due to the tiny handful of useful reasons to use it, and partially because, like <embed> and other lobstrosities, it exists heavily in the web world and HTML5 is all about cow paddies cow paths. Something about pouring asphalt over poop, or something.

Excellent explanations, as ever, @Stomme_poes. :slight_smile:

It appears that not switching to the new tab is the default behaviour in Firefox.

The W3C did deprecate it - in 1997.

Now since people writing web pages have continued to ignore notices about deprecated tags and attributes the status of all of the tags and attributes that used to be deprecated and flagged for complete removal in HTML 5 all the way back in 1997 have now had their status downgraded to obsolete which means that you still shouldn’t use them but they are not being removed completely from HTML 5 as their deprecation in 1997 indicated was to happen.

Since 1997 the opinion of the standards and the browser creators is that the user of the browser should have control of where new pages open and things such as target that try to remove options that the user would otherwise have should NOT be used.

While removing the target attribute fro what a browser recognises would not break web page (since browsers have been able to be set to ignore it for at least the last 15 years) there are a number of other tags and attributes that were flagged in 1997 that people continue to use that would break their page - even though the correct modern way of coding it would work at least as well and be easier code to maintain.

But just to be clear, the target attribute is no longer deprecated and it is not obsolete (though using them for frames/framesets would be).

And while Chris Coyier says “opening a PDF” is a bad reason, it’s pretty much required here in the Netherlands, because pretty much all Dutch sites since forever have been doing it that way, so users are very firmly trained to expect non-HTML non-webpage things to open either a new tab or window. Not using new tabs/windows on Dutch users means they regularly close the wrong thing when they’re done, because of this assumption.

User testing, if the tested users represent a very large group, probably at times has to take precedence over what we’d otherwise consider Best Practices.

I can’t say the above goes for the English-speaking countries though. I would have PDF links non-target if I were building for outside my country.

It might help to know that the actual context where I’ve used share links opening in a new tab is HTML Pencil – an application discussed [URL=“http://www.sitepoint.com/forums/showthread.php?1214526-HTML-Pencil-an-online-HTML-editor-with-real-time-preview”]here.

@Rain Lover hey thanks, context is always invaluable in discussions like these.