Reputation: 17280
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
Upvotes: 1
Views: 144
Reputation: 7771
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!
Upvotes: 1