HTML: how to force links to open in a new tab, not new window

193,263

Solution 1

There is no way to do that as the author of the HTML that a browser renders. At least not yet that I know of. Its pretty much up to the browser and its settings / preferences that are set by users themselves.

Also, you shouldn't impose this upon any user. A browser is the user's property. If a user wants to open all links in tabs or in new windows, then let the user do exactly that.

It's good that we can't do certain things. target=_blank is still abused and popups have been done to death.

Solution 2

Since I fell into this old question and then found that it is now possible (maybe this css option wasn't available then), I just want to add an update on how it can be done:

<a href="[yourlink]" target="_blank" style="target-new: tab;">Google</a>

Here are the options for the target-new style:

target-new: window | tab | none 

Didn't test the none option, maybe it uses the default browser setting.

I confirmed this for Firefox and IE7-9.

Solution 3

No, there isn't.

Solution 4

I hope this will help you

window.open(url,'_newtab');

Solution 5

I didn't try this but I think it works in all browsers:

target="_parent"

Share:
193,263

Related videos on Youtube

Moon
Author by

Moon

Updated on July 05, 2022

Comments

  • Moon
    Moon almost 2 years

    I use target="_blank" to open links in a new tab. But in IE it opens a new window which is completely logical because that is what _blank is supposed to do.

    And i don't know how target="_blank" behaves in other browsers.

    Is there something to force links to open in a new tab. If the browser supports tabs... else make a new window

    • Šime Vidas
      Šime Vidas over 13 years
      target="_blank" will open the link in a new tab in Firefox, Chrome and Opera. The other two browsers, IE and Safari will open it in a new window.
    • Matthew
      Matthew almost 12 years
      Wow, you can't even use a named target and have IE open new links in your named tab. That is, if you choose to open a link in a new tab with target="myName", subsequent links with the same target will not open in your tab but will open a new window. Sounds like a usability bug to me. Thanks IE.
  • tomsseisums
    tomsseisums over 13 years
    +1! By default most browsers open target = "_blank" in new tab, but in the old days, it would open a new window. There, I think, is no way to force links to open in new tab.
  • Šime Vidas
    Šime Vidas over 13 years
    A option to define which links should open in a new tab/window should exist. This is convenient for external links. I believe that everybody agrees that opening the link in a new tab is the desired method for most users. IE and Safari open _blank's in new windows for some reason, but that is probably a legacy behavior and has nothing to do with the user's preferences. I believe that most users even don't know how to set this behavior in their browsers.
  • Amit Patil
    Amit Patil over 13 years
    The simple solution is not to try to force your links to open in a new tab or a new window. If I want a new tab, I'll middle-click, thanks.
  • Šime Vidas
    Šime Vidas over 13 years
    @bobince What about external links? If I (as the owner of the web-site) want to provide a link to an external page (as in an article), but I don't want the visitor to navigate away (nor does the visitor (!!)), then there should be a method do accomplish this.
  • Quentin
    Quentin over 13 years
    The visitor decides when they are going to leave your site, not you, so it doesn't matter if you don't want them to leave. If the visitor doesn't want to leave then they can middle click (as @bobince already indicated). If they leave and want to come back then they have a back button … and a browser history … and a bookmarks menu … and probably the ability to simply find the site again.
  • Chris
    Chris over 12 years
    You are also forgetting that in some websites, opening the link in the same tab can be just inconvenient. Suppose you're viewing an email message, and you clicked a link, it would be really an overkill having to reload the message again. Gmail does force opening non-local links in new tabs.
  • Moin Zaman
    Moin Zaman about 12 years
    Yes, I agree that there is some logic to opening new windows / tabs for web-apps like gmail.
  • Moon
    Moon about 12 years
    user preference is one thing, but what i have learnt so far is that you need to have PHP setting different approaches for different browsers.
  • Moin Zaman
    Moin Zaman almost 12 years
    Really? How have you done it with PHP?
  • patrick
    patrick over 11 years
    It IS possible to force links opening in a new window, posted by "DNS" in this similar question: stackoverflow.com/questions/726761/…, just make sure you will in the 'specs' parameter for a window.open
  • Moon
    Moon almost 11 years
    new tab is highly dependent on user settings and browser agent. 'Open Link In A New Tab' should not be made a requirement anywhere in the design of side, learned that over the last year.
  • Mladen Janjetovic
    Mladen Janjetovic over 10 years
    I have Safari 5.1.7. (think it is latest for Windows), and this is not working for me. Maybe it is for Safari 6+? Can anyone confirm this?
  • Martin
    Martin over 10 years
    Well, it's not very well documented, I checked in caniuse.com but no info, the definition is here: w3.org/TR/css3-hyperlinks/#the-target-new. w3schools states it is not supported by any browser (w3schools.com/cssref/css3_pr_target-new.asp), but it does work on Firefox and IE at least, so it's a hard one to find info...
  • Bill Bunting
    Bill Bunting over 10 years
  • Martin
    Martin over 10 years
    @BillBunting, it's finally the same Reference I had posted. However, they say no browser supports it, but I have tested it and it is working... It's strange that such an old definition is not clearly documented...
  • earcam
    earcam almost 10 years
    Blunt, to the point, definitive answer =) I really want to edit this answer to just "No"
  • earcam
    earcam almost 10 years
    window.open returns a windowObjectReference which can be reused (alternatively the 2nd arg window name can be reused in subsequent calls to load different urls in the same tab/window). However this does not answer the question - window.open provides no way of forcing tab or window.
  • Dom Ramirez
    Dom Ramirez over 9 years
    It's insane that this definition would even be necessary. Why wouldn't they simply define another value for the target attribute?
  • Martin
    Martin over 9 years
    Well, maybe because how it works, you can create a new tab with a name (target="someTab" style="target-new: tab;") and then address to that tab with the target on some other place to get to the tab. In this way it makes sense, but it's still strange that it is nowhere documented properly...
  • neel shah
    neel shah over 9 years
    not working for me too
  • Admin
    Admin over 9 years
    It works for the newest Firefox.
  • Tsung-Ting Kuo
    Tsung-Ting Kuo over 8 years
    Thanks one really helps - to set href as javascript: !
  • Robotnik
    Robotnik about 8 years
    @neelshah et al - CSS3 did propose target-new:, but between 2013 (when Martin posted this answer) and 2016, the specification has been abandoned
  • Sergiu
    Sergiu about 8 years
    By today, this is not yet supported in any of the major browsers.
  • santos
    santos over 7 years
    I am using Chrome on Windows 7 and I can confirm that this works.
  • Paul-Hebert
    Paul-Hebert about 6 years
    It seems weird to handle this in CSS. I guess it's nice to be able to apply it via CSS selector, but it seems like this goes against the traditional separation of concerns.