String.prototype.link()

  • Revision slug: JavaScript/Reference/Global_Objects/String/link
  • Revision title: link
  • Revision id: 294618
  • Created:
  • Creator: mathiasbynens
  • 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 ".

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 "Netscape" as a hypertext link that returns the user to the Netscape home page:

var hotText="Netscape";
var URL="http://home.netscape.com/";

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="http://home.netscape.com">Netscape</a>

 

{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Objetos_globales/String/link", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/String/link", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/String/link" } ) }}

Revision Source

<p>{{ Non-standard_header() }}</p>
<h3 id="Summary" name="Summary">Summary</h3>
<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>
<h3 id="Syntax" name="Syntax">Syntax</h3>
<p><code>link(<em>hrefAttribute</em>) </code></p>
<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>&</code> characters escaped as <code>&amp;amp;</code>, and any <code>"</code> characters escaped as </code>&quot;</code>.</dd>
</dl>
<h3 id="Description" name="Description">Description</h3>
<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>
<h3 id="Examples" name="Examples">Examples</h3>
<h4 id="Example:_Using_link" name="Example:_Using_link">Example: Using <code>link</code></h4>
<p>The following example displays the word "Netscape" as a hypertext link that returns the user to the Netscape home page:</p>
<pre>
var hotText="Netscape";
var URL="http://home.netscape.com/";

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="http://home.netscape.com"&gt;Netscape&lt;/a&gt;
</pre>
<p>&nbsp;</p>
<p>{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Objetos_globales/String/link", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/String/link", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/String/link" } ) }}</p>
Revert to this revision