Kami lebih akrab dengan Window.location. Ini memiliki properti seperti protokol, nama host, host, port, pencarian, hash, href, pathname, dll. Tag A sama dengan window.location, dan memiliki properti seperti itu, yang dapat memfasilitasi kita untuk menganalisis URL, berbicara lebih sedikit, dan menambahkan kode.
fungsi parseurl (url) {var a = document.createElement ('a'); a.href = url; return {source: url, protocol: a.protocol.replace (':', ''), host: a.hostname, port: a.Port || '80', query: a.search, params: (function: (function: (function: (function: (function: (function: (function: (function: (function: (function: a. a.search.replace (/^/?/, ''). split ('&'), len = seg.length, i = 0, s; for (; i <len; i ++) {if (! Seg [i]) {lanjutan; (a.pathname.match (/// ([^//?#]+) $/i) || [, '']) [1], hash: a.hash.replace ('#', ''), jalur: A.pathname.replace (/^([^//])/, '/$ 1'), relatif: (a.href.match (/tps?: //// [^//]+(.+)/) || [, '']) [1], segmen: a.pathname.replace (/^///, ''). Split ('/')};}Alamat tes
console.log (parseurl ("http://www.w3school.com.cn/jsref/dom_obj_anchor.asp?type=2#id2"));
Hasilnya adalah sebagai berikut
Salinan kode adalah sebagai berikut:
{
File: "dom_obj_anchor.asp",
hash: "id2",
Tuan rumah: "www.w3school.com.cn",
params: {type: "2"},
Path: "/jsref/dom_obj_anchor.asp",
Port: "80",
Protokol: "http",
kueri: "? Type = 2",
relatif: "/jsref/dom_obj_anchor.asp?type=2#id2",
Segmen: [0: "jsref", 1: "dom_obj_anchor.asp"],
Sumber: http://www.w3school.com.cn/jsref/dom_obj_anchor.asp?type=2#id2
}