Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 651241 of String.prototype.link()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/String/link
  • Revision title: String.prototype.link()
  • Revision id: 651241
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment

Revision Content

{{JSRef("Global_Objects", "String")}}

Summary

The link() method creates an {{HTMLElement("a")}} HTML element that causes a string to be displayed as a hypertext link to another URL.

Syntax

str.link(url) 

Parameters

url
Any string that specifies the href attribute of the a tag; it should be a valid URL (relative or absolute), with any & characters escaped as &, and any " characters escaped as ".

Description

Use the link method to create an HTML snippet for a hypertext link. The returned string can then be added to the document via {{ Domxref("document.write") }} or {{ Domxref("element.innerHTML") }}.

Links created with the link method become elements in the links array of the document object. See {{ Domxref("document.links") }}.

Examples

The following example displays the word "MDN" as a hypertext link that returns the user to the Mozilla Developer Network.

var hotText = "MDN";
var URL = "https://developer.mozilla.org/";

console.log("Click to return to " + hotText.link(URL));

This example produces the same output as the following HTML:

Click to return to <a href="https://developer.mozilla.org/">MDN</a>

Specifications

Specification Status Comment
{{SpecName('ES6', '#sec-string.prototype.link', 'String.prototype.link')}} {{Spec2('ES6')}} Initial definition. Implemented in JavaScript 1.0.
Defined in the (normative) Annex B for Additional ECMAScript Features for Web Browsers.

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatVersionUnknown() }} {{ CompatGeckoDesktop("1.0") }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatGeckoMobile("1.0") }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}

Gecko-specific notes

  • Starting Gecko 17.0 {{geckoRelease("17")}} the " (quotation mark) is now automatically replaced by its HTML reference character &quot; in the url parameter.

See also

  • {{jsxref("String.prototype.anchor()")}}

Revision Source

<div>
 {{JSRef("Global_Objects", "String")}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <code><strong>link()</strong></code> method creates an {{HTMLElement("a")}} HTML element that causes a string to be displayed as a hypertext link to another URL.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code><em><var>str</var></em>.link(url) </code></pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
 <dt>
  <code>url</code></dt>
 <dd>
  Any string that specifies the <code>href</code> attribute of the <code>a</code> tag; it should be a valid URL (relative or absolute), with any <code>&amp;</code> characters escaped as <code>&amp;amp;</code>, and any <code>"</code> characters escaped as <code>&amp;quot;</code>.</dd>
</dl>
<h2 id="Description" name="Description">Description</h2>
<p>Use the <code>link</code> method to create an HTML snippet for a hypertext link. The returned string can then be added to the document via {{ Domxref("document.write") }} or {{ Domxref("element.innerHTML") }}.</p>
<p>Links created with the <code>link</code> method become elements in the <code>links</code> array of the <code>document</code> object. See {{ Domxref("document.links") }}.</p>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Using_link" name="Example:_Using_link">Example: Using <code>link</code></h3>
<p>The following example displays the word "MDN" as a hypertext link that returns the user to the Mozilla Developer Network.</p>
<pre class="brush:js">
var hotText = "MDN";
var URL = "https://developer.mozilla.org/";

console.log("Click to return to " + hotText.link(URL));</pre>
<p>This example produces the same output as the following HTML:</p>
<pre class="brush: html">
Click to return to &lt;a href="https://developer.mozilla.org/"&gt;MDN&lt;/a&gt;</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-string.prototype.link', 'String.prototype.link')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition. Implemented in JavaScript 1.0.<br />
    Defined in the (normative) Annex B for Additional ECMAScript Features for Web Browsers.</td>
  </tr>
 </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatGeckoDesktop("1.0") }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Android</th>
    <th>Chrome for Android</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>IE Mobile</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatGeckoMobile("1.0") }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<h2 id="See_Also" name="See_Also">Gecko-specific notes</h2>
<ul>
 <li>Starting Gecko 17.0 {{geckoRelease("17")}} the <strong>"</strong> (quotation mark) is now automatically replaced by its HTML reference character <code>&amp;quot;</code> in the <code>url</code> parameter.</li>
</ul>
<h2 id="See_Also" name="See_Also">See also</h2>
<ul>
 <li>{{jsxref("String.prototype.anchor()")}}</li>
</ul>
Revert to this revision