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

Javascript

Javascript Problem Overview


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:

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>

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

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

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

Solution 3 - Javascript

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();
}

Solution 4 - Javascript

if you want to open a link in new tab window.open has a problem, my firefox detect a popup link by using window.open' and this is not good

I was handle this by using the latest comment

var anchor = document.createElement('a');
anchor.href = 'https://example.com';
anchor.target="_blank";
anchor.click();

Solution 5 - Javascript

    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>

Solution 6 - Javascript

This code worked for me, and it was so simple using javascript

var anchor = document.createElement('a');
anchor.href = 'https://example.com';
anchor.target="_blank";

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFlamur BeqirajView Question on Stackoverflow
Solution 1 - JavascripttwinlakesView Answer on Stackoverflow
Solution 2 - JavascriptEric SanchezView Answer on Stackoverflow
Solution 3 - Javascriptmcems7View Answer on Stackoverflow
Solution 4 - JavascriptMA DeldariView Answer on Stackoverflow
Solution 5 - Javascript山茶树和葡萄树View Answer on Stackoverflow
Solution 6 - JavascriptChris Shabani MuswambaView Answer on Stackoverflow