Skip to content

Instantly share code, notes, and snippets.

@Mottie
Created July 2, 2010 15:19
Show Gist options
  • Save Mottie/461498 to your computer and use it in GitHub Desktop.
Save Mottie/461498 to your computer and use it in GitHub Desktop.
Return URL Paramater Value
/* Returns URL parameter
* url: http://www.somesite.com?name=hello&id=11111
* z = gup('name'); // z = hello; s = string, or leave blank to target window location
* Original code from Netlobo.com (http://www.netlobo.com/url_query_string_javascript.html)
*/
function gup(n,s){
n = n.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]");
var p = (new RegExp("[\\?&]"+n+"=([^&#]*)")).exec(s || window.location.href);
return (p===null) ? "" : p[1];
}
@sindresorhus
Copy link

Using regex for this is just wrong and will fail in some scenarios.

You'd better off using a tiny reusable module like:
https://github.com/sindresorhus/query-string

@Mottie
Copy link
Author

Mottie commented Nov 16, 2013

Thanks @sindresorhus! I've already found it useful! :)

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