In the front-end development, we often need to pass between the pages of the get parameters to use JS to achieve some effect, this involves a method: How to detach from the current page of the URL of the get parameters we need.
The URL that contains the get parameter is as follows: Index.php?a=11&b=abc&d=51, let me show you what to do if you obtain getting parameters.
1, get to the current page URL in the dynamic string
JS gives us a good way: Location.search, can get to the current page URL '? ' After the string.
| The code is as follows |
Copy Code |
var str = Location.search; |
2, to get the string for the first time division
| The code is as follows |
Copy Code |
var temp = Str.split (id+ "=") [1] | | "";//If a get parameter is cut, no is null
|
The ID here is the name we need to get the value of the parameter.
3, the second Division
| The code is as follows |
Copy Code |
Temp.indexof ("&") >=0? Temp.split ("&") [0]: temp;//If there is a ' & ' continue to split, there is no direct return
|
The value returned is already the corresponding parameter value of the ID we want, and the complete method is as follows:
| The code is as follows |
Copy Code |
function Getlocationval (ID) { var temp = Location.search.split (id+ "=") [1] | | ""; Return Temp.indexof ("&") >=0? Temp.split ("&") [0]: temp; } |
After we get to the parameter value, we can do the relevant DOM operation.
Let me give you an example
| The code is as follows |
Copy Code |
function Getlocationparameter (param) { var request = { Querystring:function (val) { var uri = Window.location.search; var re =new RegExp ("+ val +" = ([^&?] *) "," IG "); Return (Uri.match (re))? (decodeURI (Uri.match (re) [0].substr (Val.length + 1)): "); } } Return request. QueryString (param); } |
Attach another
| code is as follows |
copy code |
| Code Highlighting produced by Actipro Codehighlighter (freeware) http://www. Codehighlighter.com/-->//lastest: var querystrings=function () {//get URL querystring var params=document.location.search,reg=/(?: ^\?| &) (. *?) =(.*?) (? =&|$)/g,temp,args={}; while (temp=reg.exec (params))!=null args[temp[1]]=decodeuricomponent (temp[2)); return args; }; //Only one: var querystring=function (key) { return Document.location.search.match (new RegExp ("(?: ^\\?| &) "+key+" = (. *?) (? =&|$)) | | [", NULL]) [1]; } Var args=querystrings (); Alert (args.name+ | "+args.sex+" | "+args.age); //Test Link: <a href= "? name=abc&sex= male &age=12" >test getquerystring</a> |