如何将 target=“_blank” 添加到 JavaScript window.location?

2022-08-30 02:22:32

下面将目标设置为 :_blank

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

但这似乎不起作用。如何在新选项卡中启动链接?

这是我的代码:

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

答案 1

window.location设置当前窗口的 URL。要打开一个新窗口,您需要使用 .这应该有效:window.open

function ToKey(){
    var key = document.tokey.key.value.toLowerCase();
    if (key == "smk") {
        window.open('http://www.smkproduction.eu5.org', '_blank');
    } else {
        alert("Kodi nuk është valid!");
    }
}

答案 2

只需在if (key=="smk")

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