mozilla

Revision 337553 of String.prototype.link()

  • Revision slug: JavaScript/Reference/Global_Objects/String/link
  • Revision title: String.link
  • Revision id: 337553
  • Created:
  • Creator: evilpie
  • Is current revision? No
  • Comment

Revision Content

{{ Non-standard_header() }}

Summary

Creates an HTML hypertext link that requests another URL.

Method of String
Implemented in JavaScript 1.0
ECMAScript Edition none

Syntax

link(hrefAttribute) 

Parameters

hrefAttribute
Any string that specifies the HREF of the A tag; it should be a valid URL (relative or absolute), with any & characters escaped as &, and any " characters escaped as ". {{fx_minversion_inline("17")}} The " (quotation mark) is now automatically replaced by its HTML reference character "

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/";

document.write("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>

See also

Revision Source

<div>
  {{ Non-standard_header() }}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>Creates an <a class="external" href="http://www.w3.org/TR/html401/struct/links.html#adef-href">HTML hypertext link</a> that requests another URL.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en/JavaScript/Reference/Global_Objects/String"><code>String</code></a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.0</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>none</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code>link(<em>hrefAttribute</em>) </code></pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
  <dt>
    <code>hrefAttribute</code></dt>
  <dd>
    Any string that specifies the <code>HREF</code> 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>. {{fx_minversion_inline("17")}} The <strong>"</strong> (quotation mark) is now automatically replaced by its HTML reference character <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/";

document.write("Click to return to " + hotText.link(URL));</pre>
<p>This example produces the same output as the following HTML:</p>
<pre>
Click to return to &lt;a href="https://developer.mozilla.org/"&gt;MDN&lt;/a&gt;</pre>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/String/anchor" title="String.anchor | JavaScript | MDN">String.anchor</a> {{non-standard_inline}}</li>
</ul>
Revert to this revision