Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - Open Link in New Tab (SAME WINDOW)

I realize there are a couple questions already on SO for this topic, but they all seem to be quite old....just trying to get an up-to-date answer for this:

Is the standard way of opening a new tab (within the same browser window) still:

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

???

Also, I've read that it is dependent on the users config of their browser (whether the new page opens in a new tab or a new window, and also whether the new tab/window gets the focus)....I would like the focus to remain on the original tab, but I am more concerned with it opening a tab in the same browser window (keeping focus is just a bonus).

So is there a way to read/get this setting in new browsers? (chrome, ff, ie) And possibly notify the user to change their settings if they have it set to open in a new window?

like image 369
A.O. Avatar asked Oct 31 '13 18:10

A.O.


People also ask

How do I open a URL in the same window in the same tab?

open() method allows you to open URL in the browser tab or window. You can use _self value in the second parameter of the window. open() method to open URL in the same tab and in the same window with JavaScript.

Can you make the link open in a new tab JavaScript?

You need to use the _blank value in the target attribute to open the linked URL in a new tab or window. If you want to open URL with JavaScript, the open() method of Window interface is the best option. The JavaScript window. open() method opens a new browser window.

How can you open a link in a new tab browser window?

To open a link in a new tab, click the link by pressing down your middle mouse button, or right-click the link and select Open link in New Tab. If your mouse has a wheel, it can be used as a button if you press down on the wheel. These methods work in all of the major Internet browsers available for Microsoft Windows.

How do I open an iframe in a new tab?

By adding target="_blank" to the anchor, it will open it in a new tab. By adding target="_parent" to the anchor, it will open it up in the same window like a normal link.


2 Answers

I have had great success with

<a target='_blank' > 
like image 151
Giganticus Avatar answered Oct 10 '22 00:10

Giganticus


Using target="_blank" is favourable.

eg. in Chrome, anchors with target="_blank" open a new tab, however, window.open opens a whole new window.

I tried a few experiments to replace window.open with target="_blank".

Blocked by popup blocker

// create an anchor, add to body, trigger click
var a = document.createElement('a');
a.setAttribute('href', 'http://google.com');
a.setAttribute('target', '_blank');
document.body.appendChild(a);
a.click();

// hijack first anchor, change href, trigger click
var a = document.getElementsByTagName('a')[0];
a.setAttribute('href', 'http://google.com');
a.setAttribute('target', '_blank');
a.click();

// hijack first anchor that has target=_blank, change href, trigger click
var a = $('a[target="_blank"]')[0];
a.setAttribute('href', 'http://google.com');
a.click();

Allowed by popup blocker

// hijack first anchor that has target=_blank, change href, next document click triggers it
var a = $('a[target="_blank"]')[0];
a.setAttribute('href', 'http://google.com');
$(document).click(function(){
    $('a[target="_blank"]')[0].click();
});

// create an anchor, add to body, next document click triggers it
var a = document.createElement('a');
a.setAttribute('href', 'http://google.com');
a.setAttribute('target', '_blank');
document.body.appendChild(a);
$(document).click(function(){
    a.click();
});

It seems as long as the popups are triggered by a user interaction, the popup blocker allows it.

Mozilla's documentation on window.open:

https://developer.mozilla.org/en-US/docs/Web/API/window.open

like image 32
Mike Causer Avatar answered Oct 10 '22 00:10

Mike Causer