JavaScript gets the parameters in Url

  • 2020-05-07 19:10:33
  • OfStack

Recently developed projects need to use JavaScript to read the values of parameters in the Url string

Through the search for information and their own experiments, finally succeeded

The script is as follows:


<script type="text/javascript">
      function GetRequest(strName)
     {
           var strHref = window.location.href; // To obtain Url string
           var intPos = strHref.indexOf("?");  // Parameter starting position
           var strRight = strHref.substr(intPos + 1);
           var arrTmp = strRight.split("&"); // Parameter separator
           for(var i = 0; i < arrTmp.length; i++)
           {
                 var arrTemp = arrTmp[i].split("=");
                 if(arrTemp[0].toUpperCase() == strName.toUpperCase()) return arrTemp[1];
           }
           return "";
      }
</script>

Testing:


<script>
      var id=GetRequest("ID") ;
     alert(id);
</script>

If you have other methods, please let me know, this article is constantly updated

Welcome to communicate and study together


Related articles: