[javascript] How to add target="_blank" to JavaScript window.location?

The following sets the target to _blank:

if (key == "smk") {
    window.location = "http://www.smkproduction.eu5.org";
    target = "_blank";
    done = 1;
}

But this doesn't seem to work. How do I launch the link in a new tab?

Here is my code:

_x000D_
_x000D_
function ToKey() {_x000D_
  var done = 0;_x000D_
  var key = document.tokey.key.value;_x000D_
  key = key.toLowerCase();_x000D_
  if (key == "smk") {_x000D_
    window.location = "http://www.smkproduction.eu5.org";_x000D_
    target = "_blank"_x000D_
    done = 1;_x000D_
  }_x000D_
  if (done == 0) {_x000D_
    alert("Kodi nuk është valid!");_x000D_
  }_x000D_
}
_x000D_
<form name="tokey">_x000D_
  <table>_x000D_
    <tr>_x000D_
      <td>Type the key</td>_x000D_
      <td>_x000D_
        <input type="text" name="key">_x000D_
      </td>_x000D_
      <td>_x000D_
      </td>_x000D_
      <td>_x000D_
        <input type="button" value="Go" onClick="ToKey()">_x000D_
      </td>_x000D_
  </table>_x000D_
</form>
_x000D_
_x000D_
_x000D_

This question is related to javascript

The answer is


I have created a function that allows me to obtain this feature:

function redirect_blank(url) {
  var a = document.createElement('a');
  a.target="_blank";
  a.href=url;
  a.click();
}

Just use in your if (key=="smk")

if (key=="smk") { window.open('http://www.smkproduction.eu5.org','_blank'); }

_x000D_
_x000D_
    var linkGo = function(item) {_x000D_
      $(item).on('click', function() {_x000D_
        var _$this = $(this);_x000D_
        var _urlBlank = _$this.attr("data-link");_x000D_
        var _urlTemp = _$this.attr("data-url");_x000D_
        if (_urlBlank === "_blank") {_x000D_
          window.open(_urlTemp, '_blank');_x000D_
        } else {_x000D_
          // cross-origin_x000D_
          location.href = _urlTemp;_x000D_
        }_x000D_
      });_x000D_
    };_x000D_
_x000D_
    linkGo(".button__main[data-link]");
_x000D_
.button{cursor:pointer;}
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
_x000D_
_x000D_
<span class="button button__main" data-link="" data-url="https://stackoverflow.com/">go stackoverflow</span>
_x000D_
_x000D_
_x000D_