1

I am trying to do my pagination using ajax and trying to fetch the page number only from the href pagination link. I have other params in href as well.

<a href="?page=2&order_by=asc">2</a>

I need only 2 so that i can send it to ajax request.

Wasif Iqbal
  • 474
  • 1
  • 4
  • 17
  • 3
    Possible duplicate of [How can I get query string values in JavaScript?](https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – 31piy Jul 24 '17 at 06:30
  • This should help `var key = 'page'; ....getAttribute('href').split(key + '=')[1].split('&')[0]` – Rajesh Jul 24 '17 at 06:32
  • modern browsers, it would be a case of `new URL(your_a_element.href).searchParams.get('page');` – Jaromanda X Jul 24 '17 at 06:32

2 Answers2

2

you may try below

function getURLParameter(url, name) {
    return (RegExp(name + '=' + '(.+?)(&|$)').exec(url)||[,null])[1];
}

$('a').on('click',function(){
var url = $(this).attr('href');
var page_no = getURLParameter(url, 'page');
console.log(page_no);
});

Hope , it will help you

vjy tiwari
  • 843
  • 1
  • 5
  • 17
0

You can use replace to get the page number:

$('a').on('click', function() {
  var number = $(this).attr('href')
  number = number.replace(/\D/g, '');
  console.log(number);
  return false;
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="?page=2&order_by=asc">2</a>
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35