How to read query string value with JavaScript?

To read query string value from the current url with javascript you need to add a function in your head or body section.

function getQueryStringValue (key) {  
  return decodeURIComponent(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + encodeURIComponent(key).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"));  
}
Then you can alert or console the value of your query string.
Like you have this url in your query string
www.example.com/word/?key=value

now you can get the value of key string like this.
console.log(getQueryStringValue("key"));
or you can alert this
alert(getQueryStringValue("key"));

Comments