Take a look at the MDN article on window.location .
QueryString is available at window.location.search .
A solution that works in older browsers
MDN provides an example (no longer available in the above article) of how to get the value of a single key available in QueryString. Something like that:
function getQueryStringValue (key) { return decodeURIComponent(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + encodeURIComponent(key).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1")); }
In modern browsers
In modern browsers, there is a searchParams property of the URL interface that returns a URLSearchParams object. The returned object has a number of convenient methods, including the get method. So the equivalent of the above example would be:
let params = (new URL(document.location)).searchParams; let name = params.get("name");
The URLSearchParams interface can also be used to parse strings in query string format and convert them to a convenient URLSearchParams object.
let paramsString = "name=foo&age=1337" let searchParams = new URLSearchParams(paramsString); searchParams.has("name") === true; // true searchParams.get("age") === "1337"; // true
Please note that browser support in this interface is still limited, so if you need to support older browsers, use the first example or use a polyfill .
Christofer Eliasson Mar 26 2018-12-12T00: 00Z
source share