String.prototype.link() is a deprecated HTML wrapper method (same API as MDN String.prototype.link()). It returns a string that wraps your text in an <a href="..."> tag. Learn quote escaping, how it differs from anchor(), and how to replace it with the DOM — with five examples and try-it labs.
01
Kind
Instance method
02
Returns
HTML string
03
Status
Deprecated
04
Param
url (href)
05
Mutates?
No
06
Prefer
createElement
Fundamentals
Introduction
Early JavaScript included helpers that wrapped text in HTML tags — link(), anchor(), bold(), italics(), and others. link() built a hyperlink string you could drop into innerHTML.
Today those wrappers are deprecated. link() may still return a string for compatibility. Real <a href> links remain essential HTML, but you should create them with the DOM — not string methods.
💡
Learn it, don’t ship it
Study link() so you can recognize legacy code. For new pages, use document.createElement("a"), set href and textContent, then append the element.
Calling str.link(url) does not create a live DOM node. It concatenates an HTML string: an opening <a href="..."> tag, the original text, and a closing </a>.
It is an instance method on strings (auto-boxed if needed).
It returns a new string — the original is unchanged.
Double quotes inside url become ".
Ampersands (&) are not auto-escaped — prefer & when building attribute text by hand.
Prefer DOM APIs for real links in the page.
Foundation
📝 Syntax
General form of String.prototype.link:
JavaScript
str.link(url)
Parameters
url — any string used as the href attribute (relative or absolute URL). Coerced to a string if needed. MDN notes that & characters should be written as & in HTML attribute text.
Return value
A string beginning with <a href="..."> (quotes in url escaped as "), then the text of str, then </a>.
"".link("https://example.com") → empty content between tags
Type of result
Always a string (not a DOM node)
Common patterns
JavaScript
"MDN Web Docs".link("https://developer.mozilla.org/");
// 'MDN Web Docs'
"x".link('a"b');
// 'x'
// Prefer this in new code:
const elem = document.createElement("a");
elem.href = "https://developer.mozilla.org/";
elem.textContent = "MDN Web Docs";
// document.body.appendChild(elem);
Cheat Sheet
⚡ Quick Reference
Goal
Code
Legacy HTML string
str.link(url)
Result shape
<a href="...">...</a>
Escape quotes in URL
Automatic (" → ")
Modern DOM
document.createElement("a")
Set destination
elem.href = url
Use in new apps?
No — deprecated
Snapshot
🔍 At a Glance
Four facts to remember about String.link().
Returns
string
HTML markup text
Status
deprecated
Compatibility only
Tag
<a href>
Still valid HTML
Replace with
createElement
DOM, not strings
Compare
📋 link() vs anchor() vs DOM
str.link(url)
str.anchor(name)
createElement("a")
Attribute
href (destination)
name (legacy named target)
You set href / id yourself
Result
HTML string
HTML string
Live DOM element
Valid today?
Tag OK; method deprecated
name on <a> is obsolete
Yes
Recommended?
No
No
Yes
Hands-On
Examples Gallery
Examples follow MDN String.link() patterns. Use View Output or Try It Yourself for each case.
📚 Getting Started
See the exact HTML string link() builds.
Example 1 — Basic link()
MDN-style call that wraps text in <a href>.
JavaScript
const contentString = "MDN Web Docs";
contentString.link("https://developer.mozilla.org/");
// 'MDN Web Docs'
Quote escaping keeps the attribute syntactically closed. Link text is not HTML-escaped the same way — another reason to avoid injecting untrusted strings via innerHTML.
📈 Practical Patterns
Legacy injection patterns and the modern DOM replacement.
Example 3 — Injecting with innerHTML (Legacy)
How older demos used the returned string — prefer the DOM in new apps.
JavaScript
const contentString = "MDN Web Docs";
const html = contentString.link("https://developer.mozilla.org/");
// document.body.innerHTML = html; // legacy pattern — avoid in new apps
console.log(html);
// Prefer createElement("a") in new apps
A
https://developer.mozilla.org/
<a href="https://developer.mozilla.org/">MDN Web Docs</a>
How It Works
You get a live element you can style, set rel/target on, and keep separate from untrusted HTML concatenation. Prefer textContent over building markup by hand.
Applications
🚀 Common Use Cases
Reading legacy tutorials — recognize HTML wrapper methods in old samples.
Migrating old scripts — replace link() with createElement("a").
Teaching string immutability — show that methods return new strings.
Not for new apps — do not use link() to build hyperlinks in new apps.
Safer markup — prefer textContent + DOM properties over innerHTML strings.
Contrast with anchor() — link = href; anchor = obsolete name.
🧠 How link() Builds Markup
1
Start with text + URL
You call str.link(url) on a string receiver.
Input
2
Escape quotes in URL
Replace " with " inside the href value.
Escape
3
Return an HTML string
Build <a href="..."> + text + </a> — no DOM node yet.
Result
4
⚠️
Prefer the DOM
Create a real <a> with href and textContent.
Important
📝 Notes
link() is deprecated — standardized only for compatibility.
The method is deprecated; the <a href> element itself remains valid HTML.
It returns a string, not a DOM node.
Double quotes in url are escaped as ".
Ampersands are not auto-escaped — follow MDN and use & when writing attribute HTML by hand.
Other HTML wrappers (anchor(), bold(), italics(), …) share the same fate.
Avoid assigning untrusted strings to innerHTML.
Compatibility
Browser & Runtime Support
String.prototype.link() is still widely implemented for compatibility, but it is deprecated. Do not use it in new projects.
✓ Deprecated · Legacy
String.link()
Available in modern browsers for old code, but MDN recommends DOM APIs such as createElement() instead.
LegacyCompatibility only
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
link()Avoid in new code
Bottom line: Recognize link() in legacy samples. For new UI, create hyperlinks with the DOM — never rely on HTML wrapper string methods.
Wrap Up
Conclusion
String.prototype.link() builds an <a href="...">...</a> HTML string. The method is deprecated — useful for history and migrating old code — not for writing new features.
Assume string-built HTML is the best way to make links
Inject untrusted link() output via innerHTML
Expect link() to return a live element
Mix HTML wrappers with modern component frameworks
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about String.link()
Deprecated HTML wrapper — prefer createElement("a").
5
Core concepts
📝01
Returns
HTML string
API
⚠️02
Status
deprecated
Legacy
🔗03
Markup
<a href> OK
HTML
✓04
Replace
createElement
Modern
⚡05
Mutates
no
Immutable
❓ Frequently Asked Questions
String.prototype.link(url) returns an HTML string that wraps the text in an <a> tag with an href attribute — for example "Hello".link("https://example.com") returns '<a href="https://example.com">Hello</a>'.
Yes. MDN marks all HTML wrapper methods as deprecated. Prefer document.createElement("a"), set href and textContent, then append the element.
Yes. Hyperlinks are core HTML. The method is deprecated — not the <a> tag. Build links with the DOM instead of string wrappers.
Yes. Double quotes in url become " so the generated attribute stays well-formed as HTML text. Ampersands (&) are not auto-escaped — MDN recommends using & when needed.
No. Strings are immutable. link() returns a new string containing HTML markup. The original text stays the same.
Use document.createElement("a"), set elem.href and elem.textContent (or textContent + setAttribute), then appendChild. Avoid building links with innerHTML from untrusted strings.
Did you know?
link() and anchor() both produce <a> tags, but for opposite jobs: link sets href (where you go), while legacy anchor set name (where you land). Today, destinations use id on any element — not name on <a>.