184

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>

3
  • So you want to open a url in a new window?
    – Musa
    Commented Aug 27, 2013 at 22:19
  • Yepp?! only if the key is right... Commented Aug 27, 2013 at 22:20
  • where the HEAD closes?
    – Lucas
    Commented Oct 7, 2016 at 19:35

8 Answers 8

395

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!");
    }
}
3
  • 11
    @twinlakes this gets blocked in all modern browsers. Commented Nov 4, 2015 at 15:21
  • @BenRacicot well yes, popups are blocked by default and most people don't change that
    – twinlakes
    Commented Nov 5, 2015 at 0:11
  • the problem with window.open is the pop-blocked blocker
    – jmoran
    Commented Mar 21, 2018 at 19:15
87

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

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

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();
3
  • 1
    This is the best way to prevent popup Commented Oct 18, 2021 at 21:44
  • I can confirm that it works perfectly on chrome 99. Thanks!
    – Fred Klein
    Commented Mar 8, 2022 at 17:31
  • 1
    i try this but still "pop-up blocked" in google chrome
    – Vendetta
    Commented Mar 9, 2023 at 2:02
17

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();
}
1
  • this doesn't work. probably a recent browsers policy update?
    – mwm
    Commented Aug 11, 2021 at 10:28
3
<a href="http://www.smkproduction.eu5.org" onclick="window.open('http://www.smkproduction.eu5.org', '_blank');return false;"></a>

before that you can replace the event in onclick, window.open will not be blocked by the browser if called explicitly

0

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";
1
  • This doesn't seem to have anything do with the question, which is about triggering navigation directly and not using a link to do it.
    – Quentin
    Commented Mar 25, 2021 at 11:49
0

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
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented May 23 at 1:24
-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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.