Skip to content

Instantly share code, notes, and snippets.

@juliencrn
Created May 25, 2019 23:41
Show Gist options
  • Save juliencrn/251b63a9f39098ecbe315afe40f00fa3 to your computer and use it in GitHub Desktop.
Save juliencrn/251b63a9f39098ecbe315afe40f00fa3 to your computer and use it in GitHub Desktop.
Read URL using JS
// Assuming "?post=1234&action=edit"
const 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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment