Function parseurl (URL) {var A = document. createelement ('A');. href = URL; return {Source: URL, Protocol:. protocol. replace (':', ''), host:. hostname, Port:. port, query:. search, Params: (function () {var RET ={}, SEG =. search. replace (/^ \? /, ''). Split ('&'), Len = Seg. length, I = 0, s; For (; I <Len; I ++) {If (! SEG [I]) {continue;} s = seg [I]. split ('='); RET [s [0] = s [1];} return ret;}) (), file: (. pathname. match (/\/([^ \/? #] +) $/I) | [, '']) [1], hash:. hash. replace ('#', ''), path:. pathname. replace (/^ ([^ \/])/, '/$ 1'), relative: (. href. match (/TPS? : \/[^ \/] + (. +)/) | [, '']) [1], segments:. pathname. replace (/^ \//,''). split ('/')};}
Usage
VaR myurl = parseurl ('HTTP: // abc.com: 8080/DIR/index.html? Id = 255 & M = hello # Top '); myurl. file; // = 'index.html 'myurl. hash; // = 'top' myurl. host; // = 'abc. com 'myurl. query; // = '? Id = 255 & M = Hello 'myurl. params; // = Object = {ID: 255, M: Hello} myurl. path; // = '/DIR/index.html' myurl. segments; // = array = ['dir', 'index.html '] myurl. port; // = '000000' myurl. protocol; // = 'HTTP 'myurl. source; // = 'HTTP: // abc.com: 8080/DIR/index.html? Id = 255 & M = hello # Top'
Original article address:
Http://james.padolsey.com/javascript/parsing-urls-with-the-dom/
Http://blog.stevenlevithan.com/archives/parseuri