The core is the regular
The code is as follows |
Copy Code |
var url = location.href; Gets the parameter value of the keyword var es=/keyword=/; Es.exec (URL); var Result=regexp.rightcontext; |
For example:
The code is as follows |
Copy Code |
var url = location.href; Get parameter values for PID var es=/pid=/; Es.exec (URL); var Pid=regexp.rightcontext;
var aurl = "&pid=" +PID; alert (PID);//Result: &pid=37
|
example, we operate with & as a delimiter
Example
The code is as follows |
Copy Code |
function Getargs () { var args = new Object (); var query = location.search.substring (1); Get query string var pairs = Query.split ("&"); Break at Ampersand for (var i = 0; i < pairs.length; i++) { var pos = pairs[i].indexof (' = '); Look for "Name=value" if (pos = = 1) continue; If not found, skip var argname = pairs[i].substring (0,pos); Extract the name var value = pairs[i].substring (pos+1); Extract the value Value = decodeuricomponent (value); Decode it, if needed Args[argname] = value; Store as a property } return args; Return the object } |
And then set the location value based on the parameters you get.
This is the current page is actually the same as a page, and of course you can also use Document.referrer
The code is as follows |
Copy Code |
<script type= "Text/javascript" > var url = window.location; function Geturlparam (url,name) { var pattern = new RegExp ("[?]" + Name + "\= ([^&]+)", "G"); var matcher = pattern.exec (URL); var items = null; if (Matcher!= null) { try{ Items = decodeURIComponent (decodeURIComponent (matcher[1])); }catch (e) { try{ Items = decodeURIComponent (matcher[1]); }catch (e) { Items = matcher[1]; } } } return items; } Alert (Geturlparam (URL, ' type ')); Country Alert (Geturlparam (URL, ' id ')); 12 Alert (Geturlparam (URL, ' page ')); 10 </script> |
Code to optimize the
code is as follows |
copy code |
var url = " Www.111cn.net Aada=adaa&adad=adasd&sdfs=asdad#did ", Reg =/([^\=\?| \&]+) \= ([^\=\&\#]+)/g; var aa = Url.match (reg); Console.log (AA); Url.replace (reg,function ($1,$2,$3) { Console.log ($2+ "=" +$3); }); |