Brief Analysis of JS Obtaining Parameter Instance Code in url

  • 2021-06-28 10:45:19
  • OfStack

js obtains the parameter code in url as follows. The code is simple and easy to understand with comments. Please forgive me for not writing well!


function UrlSearch() {
var name, value;
var str = location.href; // Get the entire address bar 
var num = str.indexOf("?")
str = str.substr(num + 1); // Get all parameters  stringvar.substr(start [, length ]
var arr = str.split("&"); // Put each parameter in an array 
for (var i = 0; i < arr.length; i++) {
num = arr[i].indexOf("=");
if (num > 0) {
name = arr[i].substring(0, num);
value = arr[i].substr(num + 1);
this[name] = value;
}
}
} 

Example


// Links to jump pages 
url: contextPath+"/page4nui/ci2k3/prreportcheckmngt/report_check.jsp?ciPrId="+ciPrId
// Use of jumped pages 
var url = new UrlSearch();
ciPrId = url.ciPrId;

That's it.js is very practical to get url parameter knowledge. I hope you can understand it thoroughly and thoroughly!


Related articles: