How can I parse the below URL query string to get the value of the quantity parameter?
myurl/Channels/BuyCredits?quantity=2
I have tried numerous examples without success:
I just tried this example:
marvmentApp.controller("channelCreditController", function($scope, $http, $compile, $location) {
var params, test2;
$scope.parseQueryString = function() {
var objURL, str;
str = window.location.search;
objURL = {};
str.replace(new RegExp("([^?=&]+)(=([^&]*))?", "g"), function(0,ドル 1,ドル 2,ドル 3ドル) {});
objURL[1ドル] = 3ドル;
return;
return objURL;
};
params = $scope.parseQueryString();
test2 = params['quantity'];
debugger;
});
I keep getting an error 1ドル is udefined
benomatis
5,6637 gold badges39 silver badges61 bronze badges
asked Nov 1, 2014 at 18:54
1 Answer 1
Use the JavaScript location.search
property. It will return the query string of your URL (including the "?" symbol).
If the URL is http://www.example.org/index.php?param=arg
The value location.search
is ?param=arg
.
To remove the "?", use the JS replace method:
var url = "?param=arg"; // pretend that the variable "url" is the query string
url = url.replace(/[\?]/,"");
document.getElementById('output').innerHTML = "Query string is: \"" + url + "\"";
<p id="output"></p>
Hope this helps!
answered Nov 1, 2014 at 19:06
Comments
lang-js
0ドル - 3ドル
only exist within the scope of your empty functionfunction(0,ドル 1,ドル 2,ドル 3ドル) {}
. (And there are much easier ways to extract a query param.)