function code
Copy Code code 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 Code code 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; = ' 8080 '
Myurl.protocol; = ' http '
Myurl.source; = ' Http://abc.com:8080/dir/index.html?id=255&m=hello#top '
Demo Code:
<script type= "Text/javascript" > 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 /\/[^\/]+(.+)/) || [,'']) [1], Segments:a.pathname.replace (/^\//, '). Split ('/')}; var myurl = parseURL (' http://abc.com:8080/dir/index.html?id=255&m=hello#top '); alert (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; = ' 8080 ' myurl.protocol; = ' http ' myurl.source; = ' Http://abc.com:8080/dir/index.html?id=255&m=hello#top ' </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]