How to open a URL in a new Tab using JavaScript or jQuery? [duplicate] – Dev

The best answers to the question “How to open a URL in a new Tab using JavaScript or jQuery? [duplicate]” in the category Dev.

QUESTION:

How to open a URL in new tab instead of new window programatically?

ANSWER:

This is as simple as this.

window.open('_link is here_', 'name'); 

Function description:

name is a name of the window. Following names are supported:

  • _blank – URL is loaded into a new tab. This is default.
  • _parent – URL is loaded into the parent frame
  • _self – URL replaces the current page
  • _top – URL replaces any framesets that may be loaded

ANSWER:

Use window.open():

var win = window.open('http://stackoverflow.com/', '_blank');
if (win) {
    //Browser has allowed it to be opened
    win.focus();
} else {
    //Browser has blocked it
    alert('Please allow popups for this website');
}

Depending on the browsers implementation this will work

There is nothing you can do to make it open in a window rather than a tab.

ANSWER:

 var url = "http://www.example.com";
 window.open(url, '_blank');

ANSWER:

if you mean to opening all links on new tab, try to use this jquery

$(document).on('click', 'a', function(e){ 
    e.preventDefault(); 
    var url = $(this).attr('href'); 
    window.open(url, '_blank');
});