Method 1:
Use regular expressions to obtain address bar parameters (concise code, key regularity)
function getQueryString(name) { let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); let r = (1).match(reg); if (r != null) { return unescape(r[2]); }; return null; }
Calling methods
let parameter 1 = GetQueryString("parameter name 1"));
Method 2:
Split splitting method (the code is more complicated and easier to understand)
function GetRequest() { const url = ; //Get the string after the "?" character in the url let theRequest = new Object(); if (("?") != -1) { let str = (1); strs = ("&"); for(let i = 0; i < ; i ++) { theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]); } } return theRequest; }
Calling methods
let Request = new Object();
Request = GetRequest();
var parameter 1, parameter 2 ...;
Parameter 1 = Request['parameter 1'];
Parameter 2 = Request['parameter 2'];
Parameters... = Request['parameters...'];
Method 3: split split method (easy to understand, code is standard)
function getQueryVariable(variable){ let query = (1); let vars = ("&"); for (let i=0;i<;i++) { let pair = vars[i].split("="); if(pair[0] == variable){return pair[1];} } return(false); }
Calling methods
let parameter 1 = getQueryVariable("parameter name 1");
Supplement URL knowledge
Example url =https:///list/list_3_1.htm
1. (Set or get the entire URL as a string)
()
Print result: /search?q=123&page=1&type=note
2. (Set or get the protocol part of the URL)
()
Print result: http:
3. (Set or get the host part of the URL)
()
Print result:
4. (Set or get the port number associated with the URL)
()
Print result: null character (if the default 80 port is used (update: even if: 80 is added), the return value is not the default 80 but the null character)
5. (Set or get the path part with the URL (that is, the file address))
()
Print result: /search
6. (Set or get the part of the href attribute that follows the question mark)
()
Print result: ?q=123&page=1&type=note
PS: Obtain the query (parameter) 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 we believe should.
7. (Set or get the segment after the pound sign "#" in the href attribute)
()
Print result: empty characters (because there is no in the url)
The above is a detailed explanation of several ways to obtain url parameter values by js. For more information about obtaining url parameter values by js, please pay attention to my other related articles!