String.prototype.link() is deprecated and kept only for backward compatibility. For new JavaScript, create an <a> element with the DOM and set its href and text explicitly.
const text = "Visit Plus2net";
console.log(text.link("https://www.plus2net.com")); // legacy only
string.link(url)The method returns a string containing anchor markup.
const label = new String("Welcome to plus2net.com");
console.log(label.link("https://www.plus2net.com"));This explains old code you may encounter, but it is not the recommended implementation.
const link = document.createElement("a");
link.href = "https://www.plus2net.com";
link.textContent = "Visit Plus2net";
document.body.append(link);
function setSearchEngine(engine) {
const link = document.getElementById("searchLink");
link.href = engine === "google"
? "https://www.google.com"
: "https://www.yahoo.com";
}This replaces the original document.write() + radio-button approach without replacing the whole document.
When a destination comes from untrusted data, allowlist acceptable protocols and destinations. For new-tab links, use rel='noopener' where appropriate.
The original behavior is preserved in the link demo for historical reference.
Previous / String Reference Next related topic
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.