Get Query String Parameters |
Written on 05/16/20 at 03:46:52 EST by GabbyGirl |
For years we wrote gross regular expressions to get query string values but those days are gone—enter the amazing URLSearchParams API:
code: // 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" keep these tricks in your toolbox for when you need them! |
News and Comments Brought to you by: Geeks and Bloggers The comments are owned by the poster. We aren't responsible for its content.
|