First, look at a function to parse the URL, the general format of the URL is Http://domain.com/?search=a#hash
functionparseURL(URL){var a= document.CreateElement(A); /Create an A element a. href= URL; A's link is URLReturn{Source: URL, protocol: A. protocol.Replace(‘:‘,‘‘),//protocol, change colon to space host: A. hostname,//Host name Port: A. Port,//Port query: A. Search,//question mark and params after the question mark:(function(){VAR ret={}, SEG= A. Search.Replace(/^\?/,‘‘).Split(' & '),,//change the question mark inside query to a space and return as an array, separated by &. In the following example, get id=225 M=hello len= SEG. length+ N=0, S;For(I; I<len; I++){If(!seg[I]){Continue;}//no seach ignores s= SEG[I].Split(=);//Otherwise separated by an equal sign RET[s[0]]= S[1]; id=225 M=hellow}return ret; return to the above request})(), file:(A. pathname.Match(/\/([^\/?#]+) $/i)||[,‘‘])[1],//pathname is the path name, after port, search front, z regular expression is a bit messy, head big, and then analyze the hash: A. hash.Replace(‘#‘,‘‘),//#top变为top path: A. pathname.Replace(/^([^\/])/,'/$1 '),//Continue disorderly relative:(A. href.Match(/tps?:\ /\/[^\/]+(.+)/)||[, "]) [1) . Replace (/^\//, ") . Split ( '/' };}
/span>
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.segments.port//= ' 8080 ' myurl.protocol //= ' http ' myurl.source //= ' http://abc.com:8080/dir/index.html?id=255&m=hello#top '
< Span class= "token regex" >
JS in URL-related