Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a URL in a new tab (and not a new window)

I'm trying to open a URL in a new tab, as opposed to a popup window.

I've seen related questions where the responses would look something like:

window.open(url,'_blank'); window.open(url); 

But none of them worked for me, the browser still tried to open a popup window.

like image 796
Mark Avatar asked Feb 05 '11 15:02

Mark


People also ask

How do I make a link open in new tab instead of new window?

To open a link in a new browser window, hold the Shift on then click the link or right-click the link and select Open link in New Window.

Can you make a URL open in a new tab?

Configuring links to open in a separate tab is accomplished by adding text to the end of your page URL. This instructs the web browser to open the link in a new tab.

How do I make a link open in a new tab with one click?

Open Link in New Tab Generally, you can hold down the control button – or the command key on a Mac computer – to open a link in a new tab. You can also click on a link and hold down the mouse without releasing, dragging the link to the browser's tab bar to open it in a new tab.

Should an internal link open in a new tab or in the same window?

Internal vs. External Links. Links that take users to another page on the same website are internal links. Internal links should never open in new browser tabs, but rather the same tab the user is on.


2 Answers

This is a trick,

function openInNewTab(url) {  window.open(url, '_blank').focus(); }  //or just window.open(url, '_blank').focus(); 

In most cases, this should happen directly in the onclick handler for the link to prevent pop-up blockers, and the default "new window" behavior. You could do it this way, or by adding an event listener to your DOM object.

<div onclick="openInNewTab('www.test.com');">Something To Click On</div> 

http://www.tutsplanet.com/open-url-new-tab-using-javascript/

like image 78
Rinto George Avatar answered Sep 20 '22 04:09

Rinto George


Nothing an author can do can choose to open in a new tab instead of a new window; it is a user preference. (Note that the default user preference in most browsers is for new tabs, so a trivial test on a browser where that preference hasn't been changed will not demonstrate this.)

CSS3 proposed target-new, but the specification was abandoned.

The reverse is not true; by specifying certain window features for the window in the third argument of window.open(), you can trigger a new window when the preference is for tabs.

like image 31
Quentin Avatar answered Sep 22 '22 04:09

Quentin