Reputation: 4022
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
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
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
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