tags:

views:

247

answers:

3

How do I write a JavaScript function that opens a new browser tab at a specified URL? I know how to do this in In HTML using a link with target="_blank", but in my case I need to do it from within the JS function.

Thanks

A: 

You cannot specify to open in a 'new tab', but if you have a tabbed browser this behavior is configurable. For example, I believe Firefox has this behavior by default

See Tools -> Options -> Tabs -> check "open new windows in a new tab instead"

The following JavaScript will work a charm for you

<script>
    window.open("http://www.google.com", "google"); 
</script>
wiifm
A: 

You can't. Javascript has no built-in support for tabs, because not all Javascript capable browser have tabs at all (think IE6). You can at most open a new window (though that might get pop-up blocked of course).

Some browsers might show those new windows as tabs. I believe Opera does this.

Aistina
A: 
<script>
  window.open("http://stackoverflow.com/", "SO");
</script>

More info is here.

Andrejs Cainikovs
That will open the page in the current window, not a new window or a new tab.
Guffa
No. Excerpt from w3schools.com: The open() method is used to open a new browser window.
Andrejs Cainikovs
Just one time, the window will be named SO so the next time a window is open to "SO", the second link will be launch in the first window.
Rodrigo
Thanks, Rodrigo, didn't take this into account. But it is possible to drop second parameter, though.
Andrejs Cainikovs