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

1

2 Answers 2

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()

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.