sexta-feira, 18 de maio de 2018

How to Get Query String Parameters with JavaScript

// Assuming "?post=1234&action=edit"
var urlParams = new URLSearchParams(window.location.search);

console
.log(urlParams.has('post')); // true
console
.log(urlParams.get('action')); // "edit"
console
.log(urlParams.getAll('action')); // ["edit"]
console
.log(urlParams.toString()); // "?post=1234&action=edit"
console
.log(urlParams.append('active', '1')); // "?post=1234&action=edit&active=1"
URLSearchParams
https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams


You can use URLSearchParams and this polyfill to ensure that it will work on most web browsers:
https://github.com/WebReflection/url-search-params/blob/master/build/url-search-params.js


https://davidwalsh.name/query-string-javascript

See
https://stackoverflow.com/a/50417160/194717
https://stackoverflow.com/a/50417160/194717
https://stackoverflow.com/a/12151322/194717

Nenhum comentário :