String.prototype.link()

지원이 중단되었습니다: 이 기능은 더 이상 권장되지 않습니다. 일부 브라우저에서는 아직 지원할 수 있지만 관련 웹 표준에서 이미 제거되었거나 제거 과정에 있는 경우가 있으며, 호환성을 위해 유지되고 있을 수 있습니다. 사용을 피하고 가능하다면 기존 코드를 업데이트하세요. 결정을 지원할 하단의 호환성 표를 확인하세요. 이 기능은 언제든지 작동을 중단할 수 있음을 유의하세요.

String 값의 link() 메서드는 이 문자열을 <a> 요소(<a href="...">str</a>)에 포함하는 문자열을 생성해, 다른 URL에 대한 하이퍼텍스트 링크로 사용할 수 있도록 합니다.

참고 : 모든 HTML 래퍼 메서드는 더 이상 사용되지 않으며 호환성 목적으로만 표준화되었습니다. 대신 document.createElement()와 같은 DOM API를 사용하시기 바랍니다.

구문

js
link(url)

매개변수

url

<a> 요소의 href 속성을 지정하는 문자열로, 모든 & 문자가 &amp;로 이스케이프 처리된 유효한 URL(상대 또는 절대)이어야 합니다.

반환 값

<a href="url"> 시작 태그(url의 큰따옴표는 &quot;로 대체됨)로 시작하는 문자열, 그 다음 str 내용, </a> 종료 태그로 이어지는 문자열.

예제

아래 코드는 HTML 문자열을 생성한 다음 document의 body를 해당 문자열로 대체합니다.

js
const contentString = "MDN Web Docs";

document.body.innerHTML = contentString.link("https://developer.mozilla.org/");

이는 다음과 같은 HTML을 생성합니다.

html
<a href="https://developer.mozilla.org/">MDN Web Docs</a>

link()를 사용하여 HTML 텍스트를 직접 작성하는 대신 document.createElement()와 같은 DOM API를 사용해야 합니다. 아래의 예를 참고하세요.

js
const contentString = "MDN Web Docs";
const elem = document.createElement("a");
elem.href = "https://developer.mozilla.org/";
elem.innerText = contentString;
document.body.appendChild(elem);

명세서

Specification
ECMAScript® 2025 Language Specification
# sec-string.prototype.link

브라우저 호환성

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
link
Deprecated

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Deprecated. Not for use in new websites.

같이 보기