Redirect to a new tab within javascript file

Elizabeth Bradley picture Elizabeth Bradley · Aug 15, 2014 · Viewed 56.6k times · Source

I have code that sets:

top.location.href = [someurl]

But I want it to open up in a new tab. Is there anyway to have the above code include: target="_blank"?

Answer

lvogel picture lvogel · Aug 15, 2014

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".