111

I'm trying to "force" Safari or IE7 to open a new page using a new tab.

Programmatically I mean something like:

window.open('page.html','newtaborsomething');
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Ricardo Vega
  • 1,945
  • 2
  • 13
  • 12
  • Thanks of all your comments and answers, I know this is not a big deal and shouldn't be an issue in my development, but this is a client request, not an important one I just wanted to know. The problem is that IE7 and Safari won't open a popup with a tab using javascript or target attribute, true? – Ricardo Vega Jan 09 '09 at 09:42
  • 1
    I am not sure I get the point - are you developing an app, which works with IE and you want to open each new page on new tab? Or you just want to set up the IE to open new tab when clicked on a link? Or are you creating any site with more links and you want them to open separately? Pls make it clearlier. – Dungeo Jan 09 '09 at 09:31

11 Answers11

127

You can, in Firefox it works, add the attribute target="_newtab" to the anchor to force the opening of a new tab.

<a href="some url" target="_newtab">content of the anchor</a>

In javascript you can use

window.open('page.html','_newtab');

Said that, I partially agree with Sam. You shouldn't force user to open new pages or new tab without showing them a hint on what is going to happen before they click on the link.

Let me know if it works on other browser too (I don't have a chance to try it on other browser than Firefox at the moment).

Edit: added reference for ie7 Maybe this link can be useful
http://social.msdn.microsoft.com/forums/en-US/ieextensiondevelopment/thread/951b04e4-db0d-4789-ac51-82599dc60405/

y--
  • 588
  • 2
  • 10
  • 27
Eineki
  • 14,773
  • 6
  • 50
  • 59
  • 6
    Thanks for this Firefox tip, now I have updated my TargetKiller addon to remove "_newtab" and "newtab". :P –  Jan 09 '09 at 09:38
  • 1
    I really appreciate your answer, but I do know that Firefox will do it (some texts, not tested by me, remark that this is not always true, there's a 50-50 chance to get a new window or tab). I also think this shouldn't be an issue, but, you know clients :) – Ricardo Vega Jan 09 '09 at 09:40
  • 2
    This may not be the case on all builds/platforms, but Firefox 3.5 on Linux doesn't allow you to do this more than once... if you have multiple target="_newtab" links on your page, clicking on any of them will only populate a *single* new tab... that is, subsequent clicks will overwrite anything opened previously in that tab. Makes me think this is actually opening a tab *named* "_newtab", rather than being a browser-specific workaround. – Rylee Corradini Feb 19 '10 at 15:30
  • 57
    I'm pretty sure "_newtab" is meaningless, unless someone has documentation to the contrary. All you're doing is opening a window who's *name* is _newtab (which is why all subsequent links with that target open in the same window). You can target "foo" or "bar" and see the same behavior. The only well-known targets are _blank, _self, _parent, and _top. http://www.htmlcodetutorial.com/linking/_A_TARGET.html – Tom Lianza Jan 14 '11 at 19:54
65

You can't directly control this, because it's an option controlled by Internet Explorer users.

Opening pages using Window.open with a different window name will open in a new browser window like a popup, OR open in a new tab, if the user configured the browser to do so.

  • When the "Let Internet Explorer decide how popups open" option is selected, what criteria does Internet Explorer use to make this decision? – Dead.Rabit Apr 07 '16 at 07:45
48

Those of you trying to use the following:

window.open('page.html', '_newtab');

should really look at the window.open method.

All you are doing is telling the browser to open a new window NAMED "_newtab" and load page.html into it. Every new page you load will load into that window. However, if a user has their browser set to open new pages in new tabs instead of new windows, it will open a tab. Regardless, it's using the same name for the window or tab.

If you want different pages to open in different windows or tabs you will have to change the NAME of the new window/tab to something different such as:

window.open('page2.html', '_newtab2');

Of course the name for the new window/tab could be any name like page1, page2, page3, etc. instead of _newtab2.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Bob Lindabury
  • 481
  • 4
  • 2
  • 3
    knows his stuff. '_newtab' is not a reserved name. '_blank' is a special thing that opens up another unspecified and unnamed window/tab-- this one you can keep using over and over. '_newtab' is not special except that it is the specific name you gave a new tab(default) or window, which your browser keeps refreshing with whatever url you send to it. – BradChesney79 Jan 25 '19 at 23:55
13

It's up to the user whether they want to use new tabs or new windows, it isn't the business of the developer to modify this behaviour. I do not think you can do it.

Pet peeve of mine - I hate it when sites force me to open in a new window / tab - I am quite capable of making that decision for myself. Particularly when they do it in javascript - that is really unhelpful.

Sam Meldrum
  • 13,835
  • 6
  • 33
  • 40
  • 1
    +1 to Sam - new pages opening in tabs or windows is a browser (and user) specific choice. In general I hate it when any site opens a new window. – Richard Ev Jan 09 '09 at 09:28
  • In addition to this: In the "strict" definition, there's no valid attribute "target" because of not annoying the user. Try it: http://validator.w3.org – guerda Jan 09 '09 at 10:14
  • 4
    I like opening batches of tabs at home, so I am the user in that case. Just a reminder that there are exceptions. – Mark Essel Jul 05 '10 at 11:05
  • 8
    while I agree it can be annoying when sites force actions, I think it's a little unhelpful to simple state that you shouldn't do something without considering the reasons why someone might want to do it. It's common for sites to open things in new tabs, particularly external site links or when it's not helpful to have an active page replaced by new content so it's not entirely unexpected behaviour. I'd also suggest this isn't really an answer to the question and would have been better suited as a comment to the original post. – andyface Feb 20 '12 at 15:58
  • 1
    I disagree, assuming a default for the user is a design choice per web application not per browser. The browser can subscribe a default where the application does not. Also, if browsers supply the user with a way to override application defaults/browser defaults, then it's the best of both worlds. User > Application > Browser. – Derek Litz Mar 27 '12 at 18:34
  • This is not an answer to the OP's question. – user1563544 May 18 '21 at 07:41
9

Have you already tried like

var open_link = window.open('','_blank');
open_link.location="somepage.html";
Thurein Soe
  • 178
  • 3
  • 8
8

This works 100%

window.open('http://www.google.com/','_newtab' + Date.now());

Kevin Muchwat
  • 1,445
  • 18
  • 18
7

I found out in Chrome,

window.open('page.html','_newtab')

will only work once.

You can use:

window.open(ct.getNewHref(),'_newtab' + Math.floor(Math.random()*999999));

To open multiple new tabs.

blented
  • 2,699
  • 2
  • 23
  • 17
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • 10
    that's because in Chrome, _newtab is just a name and you're requesting it to open the page in the tab named "_newtab" – w00t Aug 03 '11 at 11:48
4

The code I use with jQuery:

$("a.btn_external").click(function() {
    url_to_open = $(this).attr("href");
    window.open(url_to_open, '_blank');
    return false;
});

This is useful to distinguish between the click events of a parent in a child. By using this method, you do not trigger the parent's click event.

lacroixca
  • 71
  • 3
0

If you wanted to you could use this method, which is a bit hacky, but would offer the desired functionality:

jQuery('<a/>', {
    id: 'foo',
    href: 'http://google.com',
    title: 'Become a Googler',
    rel: 'external',
    text: 'Go to Google!',
    target:'_blank',
    style:'display:none;'
}).appendTo('#mySelector');

$('#foo').click()
Xenology
  • 2,357
  • 2
  • 21
  • 39
-2

This may work if you can call a batch file (I use php with XP sp2 and IE8... you can try IE7, dunno). Use the following (or similar) in your .bat file to open Windows: Start ""C:\Progra~1\Intern~1\iexplore "http://www.site.com". There is no space between the quotation mark and C:\Progr... etc. At some point, this may begin to open new windows (i.e., target="_blank") rather than new tabs, but works up to a point; not extensively tested. To use this in a regular batch file (CMD.exe), you probably need to have a window already open. Just sharing something I stumbled across. EDITED for clarification.

-7
<a href="http://www.google.com/" target="_self">New Tab Example</a>

Works in IE7.

Regards,

Glenn

Kevin Ji
  • 10,479
  • 4
  • 40
  • 63
  • 9
    Glenn this is the opposite of what the OP asked for. _self will open the document in the same window or frame. – travega Oct 24 '11 at 20:57
  • Also It's not the way to open in programmatic way. Surprise is that how it gain +10 rep(+3/-10) with this answer. – vusan Jun 14 '17 at 10:15