Redirect to a new tab within javascript file

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

Answers


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.


Use this code

 window.open ('URL', "_newtab" );    

Need Your Help

How to check if a table contains an element in Lua?

lua set unique contains lua-table

Is there a method for checking if a table contains a value ? I have my own (naive) function, but I was wondering if something "official" exists for that ? Or something more efficient...

How to get the bigger profile picture of a facebook page

iphone objective-c ios facebook url

I am getting an image from facebook by the URL: http://external.ak.fbcdn.net/safe_image.php?d=d282e1e7c86c9270232f97ddc737df39&w=90&h=90&url=http%3A%2F%2Fi52.tinypic.com%2F2q0ixzl.jpg