Get query params from URL

PHOTO EMBED

Wed Jan 05 2022 11:27:20 GMT+0000 (Coordinated Universal Time)

Saved by @desiboli ##javascript ##js

const getParameters = (URL) => {
  URL = JSON.parse('{"' + decodeURI(URL.split("?")[1]).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') +'"}');
  return JSON.stringify(URL);
};
getParameters(window.location)
// Result: { search : "easy", page : 3 }
content_copyCOPY

You can easily retrieve query params from a URL either bypassing window.location or the raw URL goole.com?search=easy&page=3

https://blog.devgenius.io/15-javascript-codes-you-will-always-need-e8569903dd1