Skip to content

Instantly share code, notes, and snippets.

@sohelamin
Created July 16, 2015 11:57
Show Gist options
  • Save sohelamin/9cc56ffbdcb9bf5c708c to your computer and use it in GitHub Desktop.
Save sohelamin/9cc56ffbdcb9bf5c708c to your computer and use it in GitHub Desktop.
Query String Get in JavaScript
function getQueryStringValue (key) {
return unescape(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + escape(key).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"));
}
var utm_source = getQueryStringValue("utm_source");
@sohelamin
Copy link
Author

Usage:
example.com?utm_source=google
var utm_source = getQueryStringValue("utm_source");

@muralikg
Copy link

Thanks, it helped me. Consider replacing unescape with decodeURI as that seems to be deprecated.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment