JavaScript gets sample code for URL parameters and parameter values

Source: Internet
Author: User
Tags split

  This article is mainly about JavaScript to get URL parameters and parameter values of the sample code is introduced, need friends can come to the reference, I hope to help you.

  Code as follows: <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!= ') {      &NBSP ;                   New_url = "&" +d_date;                                   & nbsp         var species  = getparameter ("species", url);                       if (species!= ') {      &NBSP ;                   New_url = "& " +species;                                   & nbsp           window.open (New_url);                                   & nbsp  //javascript get the specified parameters and their corresponding values                     function Getparamete R (parastr, URL)                     {                        var result = "";                        //Get URL All parameters list data                         var str = "&" + Url.split ("?") [1];                         var paraname = parastr + "=";       &NBsp                /determine if the parameters to be acquired               &N Bsp         if (Str.indexof ("&" +paraname)!=-1)                                                   & nbsp If you want to get the parameter to the end also contains "&"                           &NBSP ; if (str.substring str.indexof (paraname), str.length) indexOf ("&")!=-1)                             {                  & nbsp            //Get the parameters to get to the end of the strings                                 var tmpstr=str.substring (Str.indexof (paraname), str.length);                                //intercept character &N between the start of the parameter and the nearest "&" Occurrence Bsp                               Result=tmpstr.sub STR (Tmpstr.indexof (paraname), Tmpstr.indexof ("&")-tmpstr.indexof (Paraname));                               {    & nbsp                       else           &NBSP ;                 {                  & nbsp               result=str.substring (Str.indexof (paraname), str.length);                               {    & nbsp                                            else     &NB Sp                                   & nbsp             result= "";                          }         & nbsp                 return (Result.replace ("&", ""));                                   & nbsp       </script>   The following example is complete by testing the   code as follows: <! 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>   &NB Sp   <meta http-equiv= "COntent-type "content=" text/html; charset=gb2312 "/>       <title>javascript get URL parameters and parameter values </title>       < Script type= "Text/javascript" >       <!--      var url = "http://www.baidu.com/?age=2 5&k=1&site=asp&abc=123; "       //javascript get the specified parameters and their corresponding values       function GetParameter (parastr, url)       {          var result = "";          //Get URL all parameter list data           var str = "&" + Url.split ("?") [1];           var paraname = parastr + "=";          //determining whether the parameters to be obtained are in           if (Str.indexof ("&" +paraname)!=-1 )           {             //If the parameter to get to the end also contains "&"   & nbsp           if (str.substring (Str.indexof (PARaname), str.length). IndexOf ("&")!=-1)               {        & nbsp        //Get the parameters to get to the end of the string  ,  ,             var tmpstr=st R.substring (Str.indexof (paraname), str.length);                  //intercept the characters from the start of the parameter to the nearest "&" occurrence of the character         &N Bsp         RESULT=TMPSTR.SUBSTR (Tmpstr.indexof (Paraname), Tmpstr.indexof ("&")-tmpstr.indexof ( Paraname));                               else   &NB Sp           {                    Result=str.subs Tring (Str.indexof (paraname), str.length);                          }             Else           {              result= "no this parameter";            }             return (Result.replace ("&", ""));        }        //calling method: var variable name = getparameter ("parameter name to get", url address)     &N Bsp var r = getparameter ("Age", url);        //test output, the result is: site=popasp       alert (R);      //According to the results obtained can use       var pname = r.split ("=") [0]; Get parameter name       var pvalue = r.split ("=") [1]; Get parameter value        //test output:       alert ("Parameter name:" + pname + "NN" + "parameter value:" + pvalue);        //Other practical applications:      //You can implement the functions you want to implement with the following methods as needed;      //var hostname = Location.hostname; Gets the current domain name (does not contain http://)      //var localurl = location.href;  //Get the current complete URL address information (including http://, domain name, path, specific file and all pass parameters)       var referurl = Document.referrer; Get the complete URL information on the previous page (including http://, domain name, path, specific file and all pass parameters)        //-->       </script> & nbsp     </head>         <body>       </body>     &NBS P </html>     
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.