javascript获取URL参数与参数值的示例代码

复制代码 代码如下:


<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获取指定参数及其对应的值 
                  function getParameter(paraStr, url) 
                  { 
                      var result = ""; 
                      //获取URL中全部参数列表数据 
                      var str = "&" + url.split("?")[1]; 
                      var paraName = paraStr + "="; 
                      //判断要获取的参数是否存在 
                      if(str.indexOf("&"+paraName)!=-1) 
                      { 
                          //如果要获取的参数到结尾是否还包含“&” 
                          if(str.substring(str.indexOf(paraName),str.length).indexOf("&")!=-1) 
                          { 
                              //得到要获取的参数到结尾的字符串 
                              var TmpStr=str.substring(str.indexOf(paraName),str.length); 
                              //截取从参数开始到最近的“&”出现位置间的字符 
                              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>


下面例子很完整 经测试

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdwgfd.html