suryanaga
suryanaga

Reputation: 4022

Incrementally increasing number in URL - JavaScript

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++);

Upvotes: 3

Views: 2304

Answers (4)

René
René

Reputation: 1

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;

Upvotes: 0

Klas Mellbourn
Klas Mellbourn

Reputation: 44437

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

Upvotes: 0

Guerra
Guerra

Reputation: 2790

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.

Upvotes: 0

adeneo
adeneo

Reputation: 318342

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

Upvotes: 3

Related Questions