We're looking for a person or people to help audit MDN to find places we could speed up. Is this you or someone you know? Check out the RFP: https://mzl.la/2IHcMiE

document.anchors Redirect 1


anchors returns a list of all of the anchors in the document.


nodeList = document.anchors;


if ( document.anchors.length >= 5 ) {
  dump("dump found too many anchors");
  window.location = "http://www.google.com";

The following is an example that auto populates a Table of Contents div with every anchor on the page:

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8" />
function init() {
  var toc = document.getElementById("toc"); 
  var i, li, newAnchor;
  for (i = 0; i < document.anchors.length; i++) {
    li = document.createElement("li");
    newAnchor = document.createElement('a');
    newAnchor.href = "#" + document.anchors[i].name;
    newAnchor.innerHTML = document.anchors[i].text;

<body onload="init()">

<h2><a name="contents">Contents</a></h2>
<ul id="toc"></ul>

<h2><a name="plants">Plants</a></h2>

<h2><a name="veggies">Veggies</a></h2>


View on jsFiddle


For reasons of backwards compatibility, the returned set of anchors only contains those anchors created with the name attribute, not those created with the id attribute.


Document Tags and Contributors

 Last updated by: Sheppy,