programing

javascript window.새 탭의 위치

lastmoon 2023. 7. 26. 22:20
반응형

javascript window.새 탭의 위치

다음을 통해 사용자를 일부 URL로 전환하는 중입니다.window.location그러나 이 URL은 브라우저의 동일한 탭에서 열립니다.새 탭에서 열기를 원합니다.window.location으로 할 수 있습니까?이 작업을 수행할 다른 방법이 있습니까?

window.open('https://support.wwf.org.uk', '_blank');

두 번째 매개 변수는 새 창에서 열리게 만드는 것입니다.제이콥 닐슨의 유익한 기사를 읽는 것을 잊지 마세요 :)

사용할 수도 있습니다.

window.open('https://support.wwf.org.uk', "_blank") || window.location.replace('https://support.wwf.org.uk');

그러면 팝업이 차단된 경우 동일한 탭에서 열립니다.

브라우저 확장자를 작성하지 않는 한 이 작업을 수행할 방법이 없을 것 같습니다.사용해 볼 수 있습니다.window.open사용자가 새 탭에서 새 창을 열 수 있도록 브라우저를 설정하기를 바랍니다.

이것은 Chrome 53에서 작동합니다.다른 곳에서 테스트한 적이 없습니다.

function navigate(href, newTab) {
   var a = document.createElement('a');
   a.href = href;
   if (newTab) {
      a.setAttribute('target', '_blank');
   }
   a.click();
}

jQuery를 사용하면 훨씬 쉽고 Chrome에서도 작동합니다.

$('#your-button').on('click', function(){
       $('<a href="https://www.some-page.com" target="blank"></a>')[0].click();    
})

팝업을 선택하는 것보다, 저는 개인적으로 이 질문 스레드 JavaScript: location에 언급된 이 솔루션이 마음에 들었습니다.href를 새 창/탭에서 여시겠습니까?

$(document).on('click','span.external-link',function(){
        var t               = $(this), 
            URL             = t.attr('data-href');        
        $('<a href="'+ URL +'" target="_blank">External Link</a>')[0].click();

    });

작업 예제.

target="_blank" 속성을 동적으로 설정해야 합니다. 그러면 새 탭에서 열리게 됩니다.document.getElementsByTagName("a")[0].setAttribute('target', '_blank')

document.getElementsByTagName("a")[0].click()

만약 당신이 새로운 창에서 열기를 원한다면, href 링크를 얻고 window.open을 사용하세요.

var link = document.getElementsByTagName("a")[0].getAttribute("href");

window.open(url, "","height=500,width=500");

위에서 두 번째 매개 변수를 _blank로 제공하지 마십시오.

언급URL : https://stackoverflow.com/questions/7554108/javascript-window-location-in-new-tab

반응형