IT

링크가 새 창을 열도록하십시오 (탭 아님).

lottoking 2020. 6. 24. 07:19
반응형

링크가 새 창을 열도록하십시오 (탭 아님). [중복]


이 질문에는 이미 답변이 있습니다.

자바 스크립트를 사용하지 않고 링크를 새 브라우저 창 (탭이 아닌)으로 여는 방법이 있습니까?


순수한 HTML사용하면 이것에 영향을 줄 수 없습니다. 모든 최신 브라우저 (= 사용자)는 과거에 많이 오용 되었기 때문에이 동작을 완전히 제어 할 수 있습니다 ...

HTML 옵션

새 창 (HTML4) 또는 새 탐색 컨텍스트 (HTML5)를 열 수 있습니다. 최신 브라우저의 브라우징 컨텍스트는 "새 창"대신 "새 탭"입니다. 당신은 그것에 영향을 미치지 않으며, 새로운 브라우저를 새 창으로 열도록 강요 할 수 없습니다.

이렇게하려면 anchor 요소 의 속성 target[1]을 사용하십시오 . 찾고있는 값은 _blank[2] 입니다.

<a href="www.example.com/example.html" target="_blank">link text</a>

자바 스크립트 옵션

javascript를 통해 새 창을 열 수 있습니다. javascript 솔루션에 대해서는 아래 Ievgen의 훌륭한 답변을 참조하십시오 .

(!) 그러나 자바 스크립트를 통해 창을 열면 (앵커 요소의 onclick 이벤트에서 수행되지 않은 경우) 팝업 차단기에 의해 차단 될 수 있습니다!

[1]이 속성은 브라우저에 탭이없고 프레임 세트 사용이 최첨단 인 시대로 거슬러 올라갑니다. 그 동안이 속성의 기능이 약간 변경되었습니다 ( MDN Docu 참조 ).

[2]와 같은 (그들이 염두에 프레임 셋으로 설계 되었기 때문에) 더 이상 많은 이해가되지 않는 일부 다른 값이있다 _parent, _self또는 _top.


그러면 JavaScript가 아닌 탭이 아닌 새 창이 열립니다.

<a href="print.html"  
    onclick="window.open('print.html', 
                         'newwindow', 
                         'width=300,height=250'); 
              return false;"
 >Print</a>

나는 그 오래된 Q를 알고 있지만 솔루션을 검색하여 여기에 도착하면 jquery를 통해 멋진 것을 얻습니다.

  jQuery('a[target^="_new"]').click(function() {
    var width = window.innerWidth * 0.66 ;
    // define the height in
    var height = width * window.innerHeight / window.innerWidth ;
    // Ratio the hight to the width as the user screen ratio
    window.open(this.href , 'newwindow', 'width=' + width + ', height=' + height + ', top=' + ((window.innerHeight - height) / 2) + ', left=' + ((window.innerWidth - width) / 2));

});

<a target="_new">새 창에서 모두 열립니다

편집하다:

첫째, 원래 코드에서 약간의 변경을 했으므로 이제는 사용자 화면 비율에 따라 새 창을 열었습니다 (가로 데스크톱의 경우).

그러나 모바일에있는 경우 새 탭에서 링크를 여는 다음 코드를 사용하는 것이 좋습니다 ( zvona가 다른 질문에 답할 수 있음 ).

jQuery('a[target^="_new"]').click(function() {
    return openWindow(this.href);
}


function openWindow(url) {

    if (window.innerWidth <= 640) {
        // if width is smaller then 640px, create a temporary a elm that will open the link in new tab
        var a = document.createElement('a');
        a.setAttribute("href", url);
        a.setAttribute("target", "_blank");

        var dispatch = document.createEvent("HTMLEvents");
        dispatch.initEvent("click", true, true);

        a.dispatchEvent(dispatch);
    }
    else {
        var width = window.innerWidth * 0.66 ;
        // define the height in
        var height = width * window.innerHeight / window.innerWidth ;
        // Ratio the hight to the width as the user screen ratio
        window.open(url , 'newwindow', 'width=' + width + ', height=' + height + ', top=' + ((window.innerHeight - height) / 2) + ', left=' + ((window.innerWidth - width) / 2));
    }
    return false;
}

당신은 이것을 시도 할 수 있습니다 :-

   <a href="some.htm" target="_blank">Link Text</a>

그리고 당신도 이것을 시도 할 수 있습니다 :-

  <a href="some.htm" onclick="if(!event.ctrlKey&&!window.opera){alert('Hold the Ctrl Key');return false;}else{return true;}" target="_blank">Link Text</a>

Browsers control a lot of this functionality but

<a href="http://www.yahoo.com" target="_blank">Go to Yahoo</a>

will attempt to open yahoo.com in a new window.

참고URL : https://stackoverflow.com/questions/12939928/make-a-link-open-a-new-window-not-tab

반응형