First, URL parsing function
Copy Code code as follows:
<script>
/**
* @param {string} URL complete URL address
* @returns a custom object for {object}
* @description Usage Example: 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 '
*/
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 ('/')
};
}
var myurl = parseURL (' http://abc.com:8080/dir/index.html?id=255&m=hello#top ');
var myurl = parseURL (' http://localhost:8080/test/mytest/toLogina.ction?m=123&pid=abc ');
alert (Myurl.path);
alert (MYURL.PARAMS.M);
alert (MYURL.PARAMS.PID);
</script>
Second, JS segmentation URL Analysis
URL: Uniform Resource Locator (uniform Resource Locator, URL)
The complete URL is composed of these parts: scheme://host:port/path?query#fragment
Copy Code code as follows:
Scheme = Communication Protocol (commonly used Http,ftp,maito, etc.)
Host = hosts (domain name or IP)
Port = port number
Path = paths
query = queries (optional, for dynamic Web pages, such as using CGI, ISAPI, php/jsp/asp/asp. NET, and other technology-made pages, can have multiple parameters, separated by the "&" symbol, and the name and value of each parameter are separated by the "=" symbol. )
fragment = Information fragment (string, used to specify a fragment in a network resource.) For example, a Web page has multiple noun interpretations, which can be directly positioned to a noun interpretation using fragment. (also known as anchor points.) )
For such a URL
Http://www.jb51.net:80/seo/?ver=1.0&id=6#imhere
We can use JavaScript to get every part of it.
1, Window.location.href
Entire URL string (in the browser is the complete address bar)
2,window.location.protocol
The protocol part of the URL
This example returns the value: http:
3,window.location.host
Host part of URL
This example returns a value of: www.jb51.net
4,window.location.port
Port portion of URL
If the default 80 port (update: Even if added: 80), then the return value is not the default of 80 but null characters
This example returns the value: ""
5,window.location.pathname
The path portion of the URL (that is, the file address)
This example returns a value of:/seo/
6,window.location.search
Query (Parameters) Section
In addition to assigning values to dynamic languages, we can also give static pages and use JavaScript to get the value of a parameter that is believed to be
This example returns a value:? ver=1.0&id=6
7,window.location.hash
Anchor Point
This example returns a value: #imhere