2013-08-27 420 views
73

下將目標設置爲_blank如何將target =「_ blank」添加到JavaScript window.location中?

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>

+0

所以你要在新窗口中打開鏈接? – Musa

+0

Yepp ?!只有當鑰匙是正確的... –

+0

'頭部'關閉? – Lucas

回答

154

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

,我做了什麼= 1; –

+0

我剛剛更新了完整的功能 – twinlakes

+0

Yeahhhh,並完成了這段代碼謝謝,這是行之有效的... –

64

if (key=="smk")

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

var linkGo = function(item) { 
 
     $(item).on('click', function() { 
 
     var _$this = $(this); 
 
     var _urlBlank = _$this.attr("data-link"); 
 
     var _urlTemp = _$this.attr("data-url"); 
 
     if (_urlBlank === "_blank") { 
 
      window.open(_urlTemp, '_blank'); 
 
     } else { 
 
      // cross-origin 
 
      location.href = _urlTemp; 
 
     } 
 
     }); 
 
    }; 
 

 
    linkGo(".button__main[data-link]");
.button{cursor:pointer;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 

 
<span class="button button__main" data-link="" data-url="https://stackoverflow.com/">go stackoverflow</span>

0

他creado UNAfunción闕我permite obtener ESTAcaracterística,SI alguien tiene alguna sugerencia對mejorarla只需使用por favor compartirla,muchas gracias

您好,我創建了一個功能,可以讓我獲得此功能,如果任何人有任何建議,以改善它,請分享,非常感謝你

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

如果你用英語保持對話,它可能會幫助你很快得到你的回覆,也可以幫助他人理解你可能完全是什麼 – Ujju

+0

謝謝, 我明白,我這樣做,所以我會說西班牙語,但是我會嘗試在這個社區以英文發表 – mcems7

相關問題