Programmatically open new pages on Tabs

JavascriptInternet Explorer-7SafariTabs

Javascript Problem Overview


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');

Javascript Solutions


Solution 1 - Javascript

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/>

Solution 2 - Javascript

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.

Solution 3 - Javascript

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.

Solution 4 - Javascript

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.

Solution 5 - Javascript

Have you already tried like

var open_link = window.open('','_blank');
open_link.location="somepage.html";

Solution 6 - Javascript

This works 100%

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

Solution 7 - Javascript

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.

Solution 8 - Javascript

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.

Solution 9 - Javascript

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()

Solution 10 - Javascript

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:\Progra1\Intern1\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.

Solution 11 - Javascript

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

Works in IE7.

Regards,

Glenn

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRicardo VegaView Question on Stackoverflow
Solution 1 - JavascriptEinekiView Answer on Stackoverflow
Solution 2 - JavascriptthenonhackerView Answer on Stackoverflow
Solution 3 - JavascriptBob LindaburyView Answer on Stackoverflow
Solution 4 - JavascriptSam MeldrumView Answer on Stackoverflow
Solution 5 - JavascriptThurein SoeView Answer on Stackoverflow
Solution 6 - JavascriptKevin MuchwatView Answer on Stackoverflow
Solution 7 - JavascriptDerek 朕會功夫View Answer on Stackoverflow
Solution 8 - JavascriptlacroixcaView Answer on Stackoverflow
Solution 9 - JavascriptXenologyView Answer on Stackoverflow
Solution 10 - Javascriptriver_mouseView Answer on Stackoverflow
Solution 11 - JavascriptGlenn MooreView Answer on Stackoverflow