Last active
December 16, 2015 19:30
-
-
Save aghouseh/5486012 to your computer and use it in GitHub Desktop.
string prototype to get all query values from a URL string or alternatively search for a specific value. pieced together from various things i found on the googles.
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
String.prototype.getParameters = function(key){ | |
var urlParams, | |
match, | |
pl = /\+/g, // Regex for replacing addition symbol with a space | |
search = /([^&=]+)=?([^&]*)/g, | |
decode = function (s) { return decodeURIComponent(s.replace(pl, ' ')); }, | |
query = this; | |
// this is hasty and fragile currently | |
if (query.indexOf('?') !== -1) { | |
query = query.split('?').slice(1).join('?'); | |
} | |
urlParams = {}; | |
while (match = search.exec(query)) { | |
urlParams[decode(match[1])] = decode(match[2]); | |
} | |
if (key) { | |
return (typeof urlParams[key]) ? urlParams[key] : false; | |
} else { | |
return urlParams; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment