-2

I simply want to get 15 in a string like:

countryNo=-1&count=15&page=2

How can I make it work?

Ibrahim Najjar
  • 19,178
  • 4
  • 69
  • 95
user1799345
  • 99
  • 2
  • 7
  • 2
    Will the string always be formatted exactly like that, or could there be other variables before it? Are you actually asking how you get a value from a querystring? The answer to what you've actually asked is `var value = "15";` so please be clearer about what you want. – Reinstate Monica Cellio Oct 18 '13 at 13:04
  • "countryNo=-1&count=15&page=2" this will serve what you need, – Robin Oct 18 '13 at 13:05
  • 1
    possible duplicate of [How can I get query string values?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values) – Barmar Oct 18 '13 at 13:06
  • Try this: http://jsfiddle.net/vp9PH/ – CD.. Oct 18 '13 at 13:10

4 Answers4

3

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];
    }
}
Arnaldo Capo
  • 178
  • 1
  • 10
2
var x = "countryNo=-1&count=15&page=2";
var n = /(?:^|&)count=(\d+)/.exec(x)[1];
Ibrahim Najjar
  • 19,178
  • 4
  • 69
  • 95
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.

andy
  • 2,369
  • 2
  • 31
  • 50
0

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'];
Tom Bowers
  • 4,951
  • 3
  • 30
  • 43