window.locationに精通しています。プロトコル、ホスト名、ホスト、ポート、検索、ハッシュ、HREF、パス名などのプロパティがあります。Aタグはwindow.locationと同じであり、そのようなプロパティを備えており、URLを分析し、話し合い、コードを追加することができます。
function parseurl(url){var a = document.createelement( 'a'); a.href = url; return {source:url、protocol:a.protocol.replace( ':'、 '')、host:a.hostname、a.hostname、a.hostname、port:a.port || '80'、query:a.search、param A.Search.Replace(/^/?/、 '')。split( '&')、len = seg.length、i = 0、s; (a.pathname.match(///([^//?#]+)$/i)|| [、 ''])[1]、hash:a.hash.replace( '#'、 '')、path:a.pathname.replace(/^([^//])/、 '/$ 1')、親relative: (a.href.match(/tps?://// [^//]+(。+)/)|| [、 ''])[1]、segments:a.pathname.replace(/^////、 '')。分割( '/')};};};}テストアドレス
console.log(parseurl( "http://www.w3school.com.cn/jsref/dom_obj_anchor.asp?tpe=2#id2"));
結果は次のとおりです
コードコピーは次のとおりです。
{
ファイル:「dom_obj_anchor.asp」、
ハッシュ: "id2"、
ホスト: "www.w3school.com.cn"、
パラメーション:{type: "2"}、
パス: "/jsref/dom_obj_anchor.asp"、
ポート: "80"、
プロトコル:「http」、
クエリ: "?type = 2"、
相対: "/jsref/dom_obj_anchor.asp?type=2#id2"、
セグメント:[0: "jsref"、1: "dom_obj_anchor.asp"]、
出典:http://www.w3school.com.cn/jsref/dom_obj_anchor.asp?type=2#id2
}