var getLinks = function () {
"use strict";
var a = document.getElementsByTagName("a"),
b = a.length,
c = 0,
d = [],
e = "",
f = location.href;
f = f.substring(0, f.lastIndexOf("/"));
for (c = 0; c < b; c += 1) {
e = a[c].getAttribute("href");
if (typeof e === "string" && e.length > 4) {
if (e.charAt(0) === "/" || e.charAt(0) === "?") {
e = f + e;
}
d.push(e);
}
}
return d.join("\n") + "\n" + d.length + " total links";
},
myLinks = getLinks(); //myLinks variable will contain the desired output.
//To output to the console just replace the line with 'return' with this code:
//console.log(d.join("\n") + "\n" + d.length + " total links");
运行此代码以在列表中返回给定页面上所有超链接的列表,每个结果在其自己的行中。
编辑:我现在将相对链接转换为绝对 URI。