Home  >  Article  >  Web Front-end  >  Get requested URL parameters in JavaScript_javascript tips

Get requested URL parameters in JavaScript_javascript tips

WBOY
WBOYOriginal
2016-05-16 18:13:371161browse

Of course, we can get the value of the parameter in the background, and then get the value of the variable in the front-end js code. For specific methods, please refer to my article: JavaScript gets the background C# variable and calls the background method.

In fact, we can also directly get the value of the request parameter in js. By using window.location.search, we can get the string starting with the ? number of the current URL. For example, the search obtained from the previous link is ?id=001. Then process the obtained string to get the value of the parameter.

Copy code The code is as follows:

function getUrlParam(name) {
var reg = new RegExp("(^|&)" name "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null)
return unescape(r[2]);
return null;
}

When calling the above method, just By passing in the name of the parameter, you can get the value of the parameter you want, such as: getUrlParam("id").
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn