0

My url suffix is like this: /Default.aspx?PageID=13132454&A=WebApp&CCID=10463&Page=2&Items=3. I am looking for a JQuery function to return the value of "Page" in the url. In this case, it should return 2. Cheers.

Alex
  • 11,551
  • 13
  • 30
  • 38
  • possible duplicate of [Get URL parameter with jQuery](http://stackoverflow.com/questions/1403888/get-url-parameter-with-jquery) - Getting a URL parameter with javascript has been well discussed on this site. [Do a search](http://stackoverflow.com/search?q=get+url+parameter+[jquery]) and you should come up with all the answers you'll need. – Luke Shaheen Aug 01 '13 at 23:40
  • A regex is probably your best bet. I don't believe there is any built in jQuery functionality to do this. – asymptoticFault Aug 01 '13 at 23:42
  • I tried the answer on stackoverflow.com/a/901144/600486, I tried getURLParameter(window.location.pathname). It alerted null. – Alex Aug 01 '13 at 23:45
  • I think the accepted answer reads the URL from `location` already. You'll want to call it with `getURLParameter('Page');`. – Jacob Aug 01 '13 at 23:47

2 Answers2

2

jQuery doesn't have any URL parsing functions that I'm aware of. This is definitely a common task, but I always end up rolling my own. Try something like this:

function parseQueryString(url) {
    var queryStringIdx = url.indexOf('?');
    var pairs = url.substr(queryStringIdx + 1)
                   .split('&')
                   .map(function(p) { return p.split('='); });
    var result = { };
    for (var i = 0; i < pairs.length; i++) {
        result[decodeURIComponent(pairs[i][0])] = decodeURIComponent(pairs[i][1]);
    }

    return result;
}

This should give you an object where the properties are the query string parameters, and the values are the values. For example, for your URL, you'll get:

{PageID: "13132454", A: "WebApp", CCID: "10463", Page: "2", Items: "3"}
Jacob
  • 77,566
  • 24
  • 149
  • 228
1

try using queryParser plugin:

Calling $.getQuery() will return an object with all the parameters with corresponding values. For location /Default.aspx?PageID=13132454&A=WebApp&CCID=10463&Page=2&Items=3 it will return:

{
    PageID:'13132454',
    A:'WebApp',
    CCID:'10463',
    Page:'2',
    Items:'3'
}
TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188