window.open with target '_blank' opens a new browser window

15,239

try this

onclick="window.open('http://www.google.com', '_self');
Share:
15,239
nrsharma
Author by

nrsharma

Working as a Software developer in one of the IT Company which delivers the reliable solutions to clients.

Updated on June 04, 2022

Comments

  • nrsharma
    nrsharma almost 2 years

    I am trying to open an link in new browser tab (not in new window).

    When I place an link on page like

    <a href="#" onclick="window.open('http://www.google.com', '_blank');"> Click Here</a>
    

    when user click on link it will open google in new browser tab. That's fine

    BUT

    $.ajax({
       type: "POST",
       url: someURL,
       data: {somedata},
       contentType: "application/json; charset=utf-8",
       dataType: "json",
       success: function (response) {
        window.open('http://www.google.com', '_blank'); // it's always open in new browser window and I want a new tab
       },
       failure: function (response) {
          alert(response);
          return;
       }
    });  
    

    When I try this within an AJAX web method, it always opens it in new browser window; but I want to open it in new tab as it's working for above example.

    I understand this is browser specific functionality but somehow I need to accomplish this.

    • Anoop Joshi P
      Anoop Joshi P almost 10 years
      which browser you are using?
    • nrsharma
      nrsharma almost 10 years
      Mostly Chrome, but our users may be using, IE 9+, FireFox, Safari...
    • Just code
      Just code almost 10 years
      var newWindow = window.open("","_blank"); declare this out of that function and then at the after success of ajax newWindow.location.href = newURL try that if succeed i want to post it as answer ;)
    • Dávid Szabó
      Dávid Szabó almost 10 years
    • nrsharma
      nrsharma almost 10 years
      @Justcode, it's same as in new window ...
    • Just code
      Just code almost 10 years
      but have u tried i was having the same issue
    • nrsharma
      nrsharma almost 10 years
      Yes I tried the same you posted in comment above..
    • nrsharma
      nrsharma almost 10 years
      @downvoter I googled alot for it but didn't found any answer. So How can this question have an answer elsewhere.
    • Abdul
      Abdul over 6 years
      add href="javascript:void(0);" on "a" tag + @Justcode answer works like charm
    • Just code
      Just code over 6 years
      @jalil glad it helps
  • A.T.
    A.T. almost 10 years
    on which browser you are using