Nous connaissons plus avec Window.Location. Il possède des propriétés telles que le protocole, le nom d'hôte, l'hôte, le port, la recherche, le hachage, le HREF, le nom de chemin, etc. La balise A est la même que Window.
fonction parseUrl (url) {var a = document.createElement ('a'); a.href = url; return {source: url, protocole: a.protocol.replace (':', ''), hôte: a.hostname, port: a.port || '80', query: a.search, params: (function () {var retl = {}, neg =. A.Search.replace (/ ^ /? /, ''). Split ('&'), Len = seg.Length, i = 0, s; for (; i <len; i ++) {if (! seg [i]) {continuant;} s = seg [i] .split ('='); ret [s [0]] = s [1];} return ret;}); (a.pathname.match (/// ([^ //? #] +) $ / i) || [, '']) [1], hash: a.hash.replace ('#', ''), chemin: a.pathname.replace (/ ^ ([^ //]) /, '/ 1 1'), Relatif: Relatif (a.href.match (/ tps?: //// [^ //] + (. +) /) || [, '']) [1], segments: a.pathname.replace (/ ^ ///, ''). Split ('/')};}Adresse de test
console.log (parseurl ("http://www.w3school.com.cn/jsref/dom_obj_anchor.asp?type=2#id2"));
Les résultats sont les suivants
La copie de code est la suivante:
{
Fichier: "dom_obj_anchor.asp",
Hash: "id2",
Hôte: "www.w3school.com.cn",
Params: {type: "2"},
Chemin: "/jsref/dom_obj_anchor.asp",
Port: "80",
protocole: "http",
requête: "? type = 2",
Relatif: "/jsref/dom_obj_anchor.asp?type=2#id2",
segments: [0: "jsref", 1: "dom_obj_anchor.asp"],
Source: http://www.w3school.com.cn/jsref/dom_obj_anchor.asp?type=2#id2
}