JS to obtain the address bar parameters of several methods summary

  • 2020-03-30 02:07:59
  • OfStack


<script>
 //Gets an array of parameters for the address bar
 function getUrlParams() {
  var search = window.location.search;
  //Write to data dictionary
  var tmparray = search.substr(1, search.length).split("&");
  var paramsArray = new Array;
  if (tmparray != null) {
   for (var i = 0; i < tmparray.length; i++) {
    var reg = /[=|^==]/;    //Split with =, but not with ==
    var set1 = tmparray[i].replace(reg, '&');
    var tmpStr2 = set1.split('&');
    var array = new Array;
    array[tmpStr2[0]] = tmpStr2[1];
    paramsArray.push(array);
   }
  }
  //Returns an array of arguments
  return paramsArray;
 }
 //Gets the parameter value by the parameter name
 function getParamValue(name) {
  var paramsArray = getUrlParams();
  if (paramsArray != null) {
   for (var i = 0; i < paramsArray.length; i++) {
    for (var j in paramsArray[i]) {
     if (j == name) {
      return paramsArray[i][j];
     }
    }
   }
  }
  return null;
 }
function test()
{
    alert("v="+getParamValue("name"));
}
var test1= function()
{
  //alert("v1="+getParamValue("name"));
  //alert("v2="+GetUrlParam("name"));
  //alert("v3="+GetUrlParms("name"));
  alert("v4="+getQuery("name"));
}
function GetUrlParam(paramName) {
 var url = document.URL; //The URL parameter, you can also use document.url to get it, too many methods
 var oRegex = new RegExp('[?&]' + paramName + '=([^&]+)', 'i');
 //var oMatch = oRegex.exec( window.top.location.search ) ; // Gets the current window's URL
 var oMatch = oRegex.exec(url);
 if (oMatch && oMatch.length > 1)
  return oMatch[1]; //The return value
 else
  return '';
}
//Get url parameters
function GetUrlParms(name) {
 var reg = new RegExp("(^|\?|&)" + name + "=([^&]*)(\s|&|$)", "i");
 if (reg.test(location.href))
  return unescape(RegExp.$2.replace(/+/g, " "));
 return "";
}
var getQuery = function (i) {

var j = location.search.match(new RegExp("[?&]" + i + "=([^&]*)(&?)", "i"));

return j ? j[1] : j

};
</script>
JS Gets the address bar parameter  :<br>
<a href="#" onclick="test1();"> Get parameter values </a>


Related articles: