source

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

manysource 2023. 8. 18. 22:46

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

특정 링크에 대한 클릭 핸들러가 있으며, 이 안에는 다음과 유사한 작업을 수행하고자 합니다.

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로 새 창을 열 수 있습니다.

이 솔루션은 또한 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>

Javascript:

$('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');

문제는window name이렇게 하면 됩니다.

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

Microsoft는 다음 인수만 허용합니다(해당 인수를 사용하는 경우).

  • _공백의
  • _미디어
  • _부모님
  • _검색
  • _자신
  • _상의

이 Microsoft 사이트 확인

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

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