0

I'm intercepting user clicks on pagination links and I need to extract only page number from that link which is always set inside page variable.

so inside my js I have ....

var a = $a.attr("href");

.. which ouputs this

/Home/GetTabData?page=2&activeTab=House

so how can I extract only this number from after page=

If it's matter keep in mind that this number can be two or three digit long and activeTab is dynamic value.

Thanks

user1765862
  • 13,635
  • 28
  • 115
  • 220

2 Answers2

2

Using some regex, you can extract it like:

var num = +(a.match(/page=(\d+)&/)[1]);

In the above code:

  1. /page=(\d+)&/ is a regex that matches the number (one or more numeric characters) between "page=" and "&". Note that we have grouped the (\d+) which is the number we are after

  2. The + prefix is for converting the extracted string into a number. This is similar to using parseInt()

techfoobar
  • 65,616
  • 14
  • 114
  • 135
0

after you have the link, in this case a, you simply search the string for 'page=' using indexOf() then make a substring from there to the next & character.

var start, end, value;
start = a.indexOf("page=");
end = a.indexOf("&", start);
value = a.substring(start+5, end);

more info on IndexOf() http://www.w3schools.com/jsref/jsref_indexof.asp

Shelby115
  • 2,816
  • 3
  • 36
  • 52