Deprecated
This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
The anchor()
method creates an <a>
HTML anchor element that is used as a hypertext target.
str.anchor(name)
name
name
attribute of the a tag to be created.A string containing an <a>
HTML element.
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.
anchor()
var myString = 'Table of Contents'; document.body.innerHTML = myString.anchor('contents_anchor');
will output the following HTML:
<a name="contents_anchor">Table of Contents</a>
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. |
if (!String.prototype.anchor) String.prototype.anchor = function(x){ return '<a name="' + x + '">' + this + '</a>' }
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | Yes | Yes | 1
|
No | Yes | Yes |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | Yes | 4 | Yes | Yes | Yes |
Server | |
---|---|
Node.js | |
Basic support | Yes |
© 2005–2018 Mozilla Developer Network and individual 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