I simply want to get 15
in a string like:
countryNo=-1&count=15&page=2
How can I make it work?
I simply want to get 15
in a string like:
countryNo=-1&count=15&page=2
How can I make it work?
Try this.
var myQuery = getQueryVariable('count');
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split("=");
if (pair[0] == variable)
return pair[1];
}
}
var x = "countryNo=-1&count=15&page=2";
var n = /(?:^|&)count=(\d+)/.exec(x)[1];
'other-count=155&count=175&page=2'.match(/(&|^)count\=([0-9]+)/)
is the code you want. It'll return an array containing: count=15
and 15
on its own.
You'll want to grab the last value from the returned array that contains the number on its own.
function extract_count(string) {
match = string.match(/[&|^]count\=([0-9]+)/)
return (match.length != 0) ? match[match.length-1] : null;
}
That will return 175 in the above example.
You could split the string up into parts and create an associative array.
var q = "countryNo=-1&count=15&page=2";
var vars = [], hash;
if(q != undefined){
q = q.split('&');
for(var i = 0; i < q.length; i++){
hash = q[i].split('=');
vars.push(hash[1]);
vars[hash[0]] = hash[1];
}
}
var count = vars['count'];