JS accurately obtains parameters in URL URL

Keywords: Attribute

1. Get the entire URL string

To get the parameters in the URL, first we need to get the entire URL string. We use: http://www.zhihua.com/wap/tmpl/member/member.html? Token = Zhihua as an example.

① Get (or set) the protocol part of the URL: window.location.protocol

  1. //window.location.protocol set or get the protocol part of URL  
  2. var test = window.location.protocol;  
  3. alert(test);  
  4. //Return to pop up: http:  

② Get (or set) the host part of the URL: window.location.host

  1. //window.location.host set or get the host part of the URL  
  2. var test = window.location.host;  
  3. alert(test);  
  4. //Back to pop up: www.zhihua.com  

③ Get (or set) the port number associated with the URL: window.location.port

  1. //window.location.port sets or gets the port number associated with the URL  
  2. var test = window.location.port;  
  3. alert(test);  
  4. //Return pop-up: empty character (if the default port 80 is used (even if: 80 is added), the return value is not the default 80 but empty character)  

④ Get (or set) the path part of the URL, that is, the file address: window.location.pathname

  1. //window.location.pathname sets or gets the path part of the URL (that is, the file address)  
  2. var test = window.location.pathname;  
  3. alert(test);  
  4. //Return to pop up: / wap/tmpl/member/member.html  

⑤ Get (or set) the part of the URL property following the question mark: window.location.search

  1. //window.location.search sets or gets the part of the attribute that follows the question mark  
  2. var test = window.location.search;  
  3. alert(test);  
  4. //Return to pop-up:? Token = Zhihua  

⑥ Gets (or sets) the segment in the URL property after the pound sign "ා": window.location.hash

  1. //window.location.hash sets or gets the segment after the pound sign in the attribute
  2. var test = window.location.hash;  
  3. alert(test);  
  4. //Return pop-up: empty character (because not in url)

⑦ Get (or set) the entire URL string: window.location.href

  1. //window.location.href set or get the entire URL string  
  2. var test = window.location.href;  
  3. alert(test);  
  4. //Back to pop up: http://www.zhihua.com/wap/tmpl/member/member.html? Token = Zhihua  
2. Get the parameter value in the URL

After the URL string is obtained, the parameter data information in the URL string is obtained. Here are several ways to get parameters:

① Get parameter value by comparing with regular expression

  1. function getQueryString(name){  
  2.     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");  
  3.     var r = window.location.search.substr(1).match(reg);  
  4.     if (r!=nullreturn r[2]; return '';  
  5. }  

② Split split method

  1. function GetRequest() {  
  2.     var url = location.search; //Get the string after the "? Character in the url  
  3.     var theRequest = new Object();  
  4.     if (url.indexOf("?") != -1) {  
  5.         var str = url.substr(1);  
  6.         strs = str.split("&");  
  7.         for (var i = 0; i < strs.length; i++) {  
  8.             theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);  
  9.         }  
  10.     }  
  11.     return theRequest;  
  12. }  
  13. var Request = new Object();  
  14. Request = GetRequest();  
  15. // var id=Request["id"];   
  16. //var parameter 1, parameter 2, parameter 3, parameter N;  
  17. //Parameter 1 = Request ['parameter 1'];  
  18. //Parameter 2 = Request ['parameter 2'];  
  19. //Parameter 3 = Request ['parameter 3'];  
  20. //Parameter N = Request ['parameter N'];  

③ How to get a single parameter

  1. function GetRequest() {  
  2.     var url = location.search; //Get the string after the "? Character in the url  
  3.     if (url.indexOf("?") != -1) {  //Judge whether there are parameters  
  4.         var str = url.substr(1); //Start with the first character because the 0 is the? To get all strings except the question mark  
  5.         strs = str.split("=");  //Use the equal sign to separate (because you know that there is only one parameter, use the equal sign to separate directly. If there are multiple parameters, use the & sign to separate, and then use the equal sign to separate)  
  6.         alert(strs[1]);     //Pop up the first parameter directly (if there are more than one parameter, you need to loop)  
  7.     }  
  8. }  

Posted by Maiku on Mon, 30 Mar 2020 23:07:04 -0700