Created
October 29, 2015 16:31
-
-
Save greyby/b4cefafab15966ca4d99 to your computer and use it in GitHub Desktop.
get url parameter by name with javascript
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
var getParameterByName = function (field, url) { | |
var href = url ? url : window.location.search; | |
// replace '[' and ']' character for '\[' and '\]' | |
field = field.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); | |
var regex = new RegExp("[\\?&]" + field + "=([^&#]*)"), | |
results = regex.exec(href); | |
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); | |
} |
var getParameterByName = function (field, url) {
var href = url ? url : window.location.search;
field = field.replace(/[\[]/g, "\\[").replace(/[\]]/g, "\\]");
var regex = new RegExp("[\\?&]" + field + "=([^&#]*)"),
results = regex.exec(href);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This function is from stackoverflow.I think it has some problems.It can't handle these urls.
http://example.com/t?field1=value1&field1=value2&field2=value3
http://example.com/t?a+b=bbb