Programing

새 창에서 링크를 열려면 어떻게합니까?

lottogame 2020. 8. 29. 11:50
반응형

새 창에서 링크를 열려면 어떻게합니까?


특정 링크에 대한 클릭 처리기가 있는데 그 안에 다음과 비슷한 작업을 수행하고 싶습니다.

window.location = url

실제로 새 창에서 URL을 열려면 이것이 필요합니다. 어떻게해야합니까?


다음을 좋아할 수 있습니다.

window.open('url', 'window name', 'window settings')

jQuery :

$('a#link_id').click(function(){
  window.open('url', 'window name', 'window settings');
  return false;
});

또한 설정할 수 target_blank실제로.


클릭 핸들러 내에서 타겟을 강제하는 방법은 다음과 같습니다.

$('a#link_id').click(function() {
    $(this).attr('target', '_blank');
});

당신은 사용해야 할 것입니다 window.open(url);

참조 :
http://www.htmlcodetutorial.com/linking/linking_famsupp_120.html
http://www.w3schools.com/jsref/met_win_open.asp


이를 위해 jquery prop () 메서드를 사용할 수도 있습니다.

$(function(){
  $('yourselector').prop('target', '_blank');
}); 

이 문제에 대한 흥미로운 해결책을 찾았습니다. 웹 서비스의 반환을 기반으로 정보를 포함하는 스팬을 만들고있었습니다. 클릭하면 "a"가 클릭을 캡처 할 수 있도록 범위 주위에 링크를 배치하려고 생각했습니다.

하지만 스팬으로 클릭을 캡처하려고했기 때문에 스팬을 만들 때 왜 이렇게하지 않는지 생각했습니다.

var span = $('<span id="something" data-href="'+url+'" />');

그런 다음 'data-href'속성을 기반으로 링크를 생성 한 범위에 클릭 핸들러를 바인딩했습니다.

span.click(function(e) {
    e.stopPropagation();
    var href = $(this).attr('data-href');
    var link = $('<a href="http://' + href + '" />');
    link.attr('target', '_blank');
    window.open(link.attr('href'));
});

이를 통해 성공적으로 범위를 클릭하고 적절한 URL로 새 창을 열 수있었습니다.


뭐가 문제 야 <a href="myurl.html" target="_blank">My Link</a>? 자바 스크립트가 필요하지 않습니다 ...


이 솔루션은 또한 url이 비어 있고 빈 링크를 비활성화 (회색)하는 경우를 고려했습니다.

$(function() {
  changeAnchor();
});

function changeAnchor() {
  $("a[name$='aWebsiteUrl']").each(function() { // you can write your selector here
    $(this).css("background", "none");
    $(this).css("font-weight", "normal");

    var url = $(this).attr('href').trim();
    if (url == " " || url == "") { //disable empty link
      $(this).attr("class", "disabled");
      $(this).attr("href", "javascript:void(0)");
    } else {
      $(this).attr("target", "_blank");// HERE set the non-empty links, open in new window
    }
  });
}
a.disabled {
  text-decoration: none;
  pointer-events: none;
  cursor: default;
  color: grey;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<a name="aWebsiteUrl" href="http://www.baidu.com" class='#'>[website]</a>
<a name="aWebsiteUrl" href=" " class='#'>[website]</a>
<a name="aWebsiteUrl" href="http://www.alibaba.com" class='#'>[website]</a>
<a name="aWebsiteUrl" href="http://www.qq.com" class='#'>[website]</a>


클릭 이벤트에 대한 이벤트 핸들러 함수 내에서 AJAX 요청실행 하려는 경우주의 하십시오. 어떤 이유로 Chrome (및 기타 브라우저)은 새 탭 / 창을 열지 않습니다.


이것은 아주 좋은 수정은 아니지만 작동합니다.

CSS :

.new-tab-opener
{
    display: none;
}

HTML :

<a data-href="http://www.google.com/" href="javascript:">Click here</a>
<form class="new-tab-opener" method="get" target="_blank"></form>

자바 스크립트 :

$('a').on('click', function (e) {    
    var f = $('.new-tab-opener');
    f.attr('action', $(this).attr('data-href'));
    f.submit();
});

라이브 예 : http://jsfiddle.net/7eRLb/


Microsoft IE는 두 번째 인수로 이름을 지원하지 않습니다.

window.open('url', 'window name', 'window settings');

Problem is window name. This will work:

window.open('url', '', 'window settings')

Microsoft only allows the following arguments, If using that argument at all:

  • _blank
  • _media
  • _parent
  • _search
  • _self
  • _top

Check this Microsoft site

참고URL : https://stackoverflow.com/questions/2827637/how-can-i-open-a-link-in-a-new-window

반응형