2

I have a URL like http://blog.com/post/1 and I need a function which updates the number at the end of it, for pagination.

So far I have:

window.location(document.URL++);
Avindra Goolcharan
  • 4,032
  • 3
  • 41
  • 39
suryanaga
  • 3,728
  • 11
  • 36
  • 47

4 Answers4

3
var url  = window.location.href.split('/'),
    page = parseInt(url.pop(), 10);

// to go to next page, increment page number and join with URL

window.location.href = url.join('/') +'/'+ (++page);

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
0

You can do this with:

var url = document.URL;
var pagenumber = url.substr(url.length-1);
window.location = '/post/'+pagenumber++;

But it's a hacky, you can do your project struture better to don't need do this.

Guerra
  • 2,792
  • 1
  • 22
  • 32
  • @sanjaypoyzer I don't know if getting the number on url is the best way to do. Becouse your code will be restricted to this format. If you need to change url, you need to change lots of code. And you can handle just 1 digit with this. – Guerra May 27 '13 at 20:12
  • 1
    @sanjaypoyzer it does not handle pagenumbers with more than one digit. – Klas Mellbourn May 27 '13 at 20:12
  • incrementing the page number would be a good idea, no use in going to the same page ? – adeneo May 27 '13 at 20:14
  • Ah it's definitely an issue that it doesn't handle pages with more than one digit. Can it be done without this fault? – suryanaga May 27 '13 at 20:19
  • 2
    Saving the worst for last, `location` is not a function ! – adeneo May 27 '13 at 20:21
  • again, you'r right @adeneo, working a lot my eyes are failing with me. Fell free to edit the answer too. – Guerra May 27 '13 at 20:23
  • You can get the "/" and assume, all after '/' is page number, but still wrong. – Guerra May 27 '13 at 20:25
  • @adaneo I was using the code from [here](http://stackoverflow.com/questions/846954/change-url-and-redirect-using-jquery) – suryanaga May 27 '13 at 20:28
0
var url = window.location.href; /* e.g. http://blog.com/post/1 */
var pagenumberString = url.match(/\d+/)[0];
window.location.href = url.substr(0, url.length - pagenumberString.length)
                       + (parseInt(pagenumberString, 10) + 1);
Klas Mellbourn
  • 42,571
  • 24
  • 140
  • 158
0

Increment the last number in URL (usually the page number) using a bookmark :

javascript:url=window.location.href;
newurl=url.replace(/(\d+)([^0-9]*)$/, function(t,x,y){return parseInt(x,10)+1+y});
window.location.href=newurl;
René
  • 1