Javascript gets sample code for URL parameters and parameter values

  • 2020-03-30 01:00:01
  • OfStack


<script>
                  function goto_url(url){
                      var new_url = "http://shop.usteel.com/index.php?app=list_release";
                      var d_date  = getParameter("date",url);
                      if(d_date != '' ){
                          new_url += "&"+d_date;
                      }
                      var species  = getParameter("species",url);
                      if(species != ''){
                          new_url += "&"+species;
                      }

                      window.open(new_url);

                  }
                  //Javascript gets the specified parameter and its corresponding value & NBSP;
                  function getParameter(paraStr, url)  
                  {  
                      var result = "";  
                      //Gets all parameter list data & NBSP; in the URL;
                      var str = "&" + url.split("?")[1];  
                      var paraName = paraStr + "=";  
                      //Determining whether the parameter to be obtained exists & PI;
                      if(str.indexOf("&"+paraName)!=-1)  
                      {  
                          //If the parameter you want to get also contains an "&" & PI to the end;
                          if(str.substring(str.indexOf(paraName),str.length).indexOf("&")!=-1)  
                          {  
                              //Gets the string & PI that takes the argument to the end;
                              var TmpStr=str.substring(str.indexOf(paraName),str.length);  
                              //Intercepts the character & NBSP; from the beginning of the argument to the nearest & position;
                              result=TmpStr.substr(TmpStr.indexOf(paraName),TmpStr.indexOf("&")-TmpStr.indexOf(paraName));    
                          }  
                          else  
                          {    
                              result=str.substring(str.indexOf(paraName),str.length);    
                          }  
                      }    
                      else  
                      {    
                          result="";    
                      }    
                      return (result.replace("&",""));    
                  }  
                  </script>

The following example is very well tested

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    <html xmlns="http://www.w3.org/1999/xhtml">  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
    <title>javascript To obtain URL Parameter and parameter value </title>  
    <script type="text/javascript">  
    <!--  
    var url = "http://www.baidu.com/?age=25&k=1&site=asp&abc=123;"  

    //Javascript gets the specified parameter and its corresponding value & NBSP;
    function getParameter(paraStr, url)  
    {  
        var result = "";  
        //Gets all parameter list data & NBSP; in the URL;
        var str = "&" + url.split("?")[1];  
        var paraName = paraStr + "=";  
        //Determining whether the parameter to be obtained exists & PI;
        if(str.indexOf("&"+paraName)!=-1)  
        {  
            //If the parameter you want to get also contains an "&" & PI to the end;
            if(str.substring(str.indexOf(paraName),str.length).indexOf("&")!=-1)  
            {  
                //Gets the string & PI that takes the argument to the end;
                var TmpStr=str.substring(str.indexOf(paraName),str.length);  
                //Intercepts the character & NBSP; from the beginning of the argument to the nearest & position;
                result=TmpStr.substr(TmpStr.indexOf(paraName),TmpStr.indexOf("&")-TmpStr.indexOf(paraName));    
            }  
            else  
            {    
                result=str.substring(str.indexOf(paraName),str.length);    
            }  
        }    
        else  
        {    
            result=" Without this parameter ";    
        }    
        return (result.replace("&",""));    
    }  

    //Var variable name = getParameter(" parameter name to be obtained ", URL address)& PI;
    var r = getParameter("age",url);  

    //Test output: site=popasp 
    alert(r);  
    //Depending on the result, you can use & NBSP;
    var pName = r.split("=")[0]; //Gets the parameter name & NBSP;
    var pValue = r.split("=")[1]; //Gets the parameter value & PI;

    //Test output: & NBSP;
    alert(" Parameter name: " + pName + "nn" + " Parameter values: " + pValue);  

    //Other practical applications: & NBSP;
    //According to the need, with the following methods to achieve their own functions;  
    //var hostname = location.hostname; // Get the current domain name ( Does not contain http://)  
    //var localurl = location.href;   // Gets the current complete URL Address information ( contains http:// , domain name, path, specific file and all pass parameters )  
    //var referurl = document.referrer; // Get the full version of the previous page URL information ( contains http:// , domain name, path, specific file and all pass parameters )  

    //-->  
    </script>  
    </head>  

    <body>  
    </body>  
    </html> 

Related articles: