1. URL parsing function
The code copy is as follows:
<script>
/**
*@param {string} url Complete URL address
*@returns {object} Custom 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 return;
})(),
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>
2. JS segmented URL analysis
URL: Uniform Resource Locator (URL)
The complete URL consists of these parts: scheme://host:port/path?query#fragment
The code copy is as follows:
scheme = communication protocol (commonly used http, ftp, maito, etc.)
host = host (domain name or IP)
port = port number
path = path
query = query (optional, used to pass parameters to dynamic web pages (such as web pages made using CGI, ISAPI, PHP/JSP/ASP/ASP.NET and other technologies). There can be multiple parameters, separated by the "&" symbol, and the name and value of each parameter are separated by the "=" symbol.)
fragment = information fragment (a string used to specify fragments in a network resource. For example, if there are multiple noun explanations in a web page, you can use fragment to directly locate a certain term explanation. (Also known as an anchor.))
For such a URL
//www.VeVB.COM:80/seo/?ver=1.0&id=6#imhere
We can get the various parts of it in javascript
1, window.location.href
The entire URl string (the complete address bar in the browser)
2, window.location.protocol
The protocol part of the URL
This example returns the value: http:
3,window.location.host
The host part of the URL
Return value in this example: www.VeVB.COM
4,window.location.port
The port part of the URL
If the default 80 port is used (update: even if:80 is added), the return value is not the default 80 but the empty character
This example returns the value: ""
5, window.location.pathname
The path part of the URL (that is the file address)
This example returns the value: /seo/
6,window.location.search
Query (parameters) part
In addition to assigning values to dynamic languages, we can also give static pages and use javascript to obtain the value of the parameter that is believed to be.
Return value in this example:?ver=1.0&id=6
7,window.location.hash
Anchor point
Return value in this example: #imhere