tech-tips/Языки программирования/JavaScript/Получаем GET-параметры в JavaScript (без jQuery).md

28 lines
720 B
Markdown
Raw Normal View History

```JavaScript
window.getQueryParam = (function(a) {
if (a == "") return {};
var b = {};
for (var i = 0; i < a.length; ++i)
{
var p=a[i].split('=', 2);
if (p.length == 1)
b[p[0]] = "";
else
b[p[0]] = decodeURIComponent(p[1].replace(/\\+/g, " "));
}
return b;
})(window.location.search.substr(1).split('&'));
```
**Пример:** `?topic=123&name=query+string`
**Использование:**
```JavaScript
var qs = window.getQueryParam;
qs["topic"]; // 123
qs["name"]; // query string
qs["nothere"]; // undefined (object)
```
[Источник](https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript)