JavaScript window.location에 target =“_ blank”를 추가하는 방법은 무엇입니까?


118

다음은 대상을로 설정합니다 _blank.

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>


새 창에서 URL을 열고 싶습니까?
Musa

응?! 열쇠는 바로 경우에만 ...
Flamur Beqiraj

HEAD닫는 곳은 어디 입니까?
루카스

답변:


266

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

8
@twinlakes 이것은 모든 최신 브라우저에서 차단됩니다.
Ben Racicot 2015

@BenRacicot 예, 팝업은 기본적으로 차단되며 대부분의 사람들은 변경하지 않습니다
twinlakes

window.open의 문제는 팝업 차단 차단제입니다
jmoran

76

당신의 if (key=="smk")

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

6

이 기능을 얻을 수있는 기능을 만들었습니다.

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

0

    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>

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.