如何添加target =“_ blank”到JavaScript window.location?

以下将目标设置为“_blank”:

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

但是这似乎不起作用。 如何在新标签中启动链接?

这是我的代码:

 <HEAD> <script LANGUAGE="JavaScript"> <!-- Begin 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!"); } } // End --> </SCRIPT> <!-- STEP TWO: Paste this code into the BODY of your HTML document --> <BODY> <center> <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> 

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!"); } } 

只需使用你的if (key=="smk")

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