How to add target = "_ blank" to JavaScript.location window?

The following sets the target for "_blank":

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

But this does not seem to work. How to launch a link in a new tab?

Here is my code:

 <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> 
+62
javascript
Aug 27 '13 at 22:14
source share
3 answers

window.location sets the URL of your current window. To open a new window, you need to use window.open . This should work:

 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!"); } } 
+140
Aug 27 '13 at 22:19
source share

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

 if (key=="smk") { window.open('http://www.smkproduction.eu5.org','_blank'); } 
+62
Aug 27 '13 at 22:20
source share

  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
Dec 07 '17 at 6:19 06:19
source share



All Articles