Redirect to a new tab within javascript file Redirect to a new tab within javascript file javascript javascript

Redirect to a new tab within javascript file


Use the method window.open(url, target) to open a new window (it depends on the browser or the user's settings whether the URL is opened in a new window or tab):

window.open('http://stackoverflow.com', '_blank');

For more information about window.open(), read the documentation at w3schools.

Please note: Randomly opening a new window (or tab) isn't allowed in most browsers, because it is then treated as an "unwanted popup".


If you're doing this in response to a user's action (such as a click), you can use window.open:

window.open("someurl", "_blank");

On most browsers with default settings, that will open a new tab rather than a new window. The user is, of course, in charge and can change the settings so it's a new window instead.

You cannot do this with a decent browser if it's not in response to a user's click, so that web pages cannot open tabs randomly.


Here is alternative for window.open - working example here (stackoverflow snippets not allow to opening)

let a= document.createElement('a');a.target= '_blank';a.href= 'https://example.com/';a.click();