Created
July 2, 2010 15:19
-
-
Save Mottie/461498 to your computer and use it in GitHub Desktop.
Return URL Paramater Value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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