String.prototype.anchor()

The anchor() method creates an <a> HTML anchor element that is used as a hypertext target.

Syntax

JavaScript
<var>str</var>.anchor(<var>name</var>)

Parameters

name
A string representing the name attribute of the a tag to be created.

Return value

A string containing an <a> HTML element.

Description

Use the anchor() method to programmatically create and display an anchor in a document.

In the syntax, the text string represents the literal text that you want the user to see. The name parameter string represents the name attribute of the <a> element.

Anchors created with the anchor() method become elements in the document.anchors array.

Examples

Using anchor()

JavaScript
var myString = 'Table of Contents';

document.body.innerHTML = myString.anchor('contents_anchor');

will output the following HTML:

HTML
<a name="contents_anchor">Table of Contents</a>

Specifications

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'String.prototype.anchor' in that specification.
Standard Initial definition. Implemented in JavaScript 1.0. Defined in the (normative) Annex B for Additional ECMAScript Features for Web Browsers.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) 1.0 (1.7 or earlier) [1] (Yes) (Yes) (Yes)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) 1.0 (1.0) [1] (Yes) (Yes) (Yes)

[1] Starting with Gecko version 17, the " (quotation mark) is replaced by its HTML reference character &quot; in strings supplied for the name parameter.

See also

License

© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/string/anchor

HTML wrapper methods JavaScript Method Prototype Reference String