Wir sind mit Windows.location besser vertraut. Es verfügt über Eigenschaften wie Protokoll, Hostname, Host, Port, Suche, Hash, HREF, Pathname usw. Das A -Tag ist mit Windows.Location das gleiche und verfügt über solche Eigenschaften, die uns erleichtern können, die URL zu analysieren, weniger zu sprechen und Code hinzuzufügen.
Funktion parseurl (url) {var a = document.createelement ('a'); a.tref = url; return {Quelle: url, protokol: a.protocol.replace (':', ''), host: a.hostname, port: a.port || '80', Abfrage: A.Search, Params, (function (function A.Search.Replace (/^/?/, ''). Split ('&'), len = Seg.Length, i = 0, s; für (; i <len; i ++) {if (! Seg [i]) {Fortsetzung; (A.PathName.Match (/// ([^//?#]+) $/i) || [, '']) [1], Hash: a.hash.replace ('#', ''), Pfad: a.pathname.replace (/^([^//])/, '/$ 1'), Relativ: relativ: (a.href.match (/tps?Testadresse
console.log (parseurl ("http://www.w3school.com.cn/jsref/dom_obj_anchor.asp?type=2#id2"));
Die Ergebnisse sind wie folgt
Die Codekopie lautet wie folgt:
{
Datei: "Dom_obj_anchor.asp",
Hash: "ID2",
Host: "www.w3school.com.cn",
Params: {Typ: "2"},
Pfad: "/jsref/dom_obj_anchor.asp",
Port: "80",
Protokoll: "http",
Abfrage: "? Typ = 2",
Verwandter: "/jsref/dom_obj_anchor.asp?type=2#id2",
Segmente: [0: "Jsref", 1: "Dom_obj_anchor.asp"],
Quelle: http://www.w3school.com.cn/jsref/dom_obj_anchor.asp?type=2#id2
}