Function Code
Copy codeThe Code is as follows:
Function parseURL (url ){
Var a = document. createElement ('A ');
A. href = url;
Return {
Source: url,
Protocol: a. protocol. replace (':',''),
Host: a. hostname,
Port: a. port,
Query: a. search,
Params: (function (){
Var ret = {},
Seg = a. 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: (a. pathname. match (// ([^ \/? #] +) $/I) | [, '']) [1],
Hash: a. hash. replace ('#',''),
Path: a. pathname. replace (/^ ([^ \/])/, '/$1 '),
Relative: (a. href. match (/tps? : \/[^ \/] + (. +)/) | [, '']) [1],
Segments: a. pathname. replace (/^ \ //, ''). split ('/')
};
}
Usage:
Copy codeThe Code is as follows:
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; // = '20140901'
MyURL. protocol; // = 'http'
MyURL. source; // = 'HTTP: // abc.com: 8080/dir/index.html? Id = 255 & m = hello # top'
DEMO code:
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]