I have a little issue that I couldn't figure out. I have a slider and I want to change url when I click
next buttons. Here are my codes below but it doesn't work correctly. Every time I click the button, url appending instead of replacing.
var url = window.location.href; url = url + "page/" + 1; // this number is dynamic actually window.location.href = url;
For instance it displays;
stackoverflow.com/page and I clicked
stackoverflow.com/page/1 and again
But I just want to replace it
How can I fix it? Thank you for your help.
window.location.href returns the entire url...
if you just want to add the "/page/1"
var url = window.location.origin; url = url + "/page/" + 1; // this number is dynamic actually window.location.href = url;
though window.location.origin is undefined in windows 10
you could just replace the /page/#number#
var url = window.location.href; url = url .replace(new RegExp("/page/[0-9]"), "/page/2") window.location.href = url;
All you want is a leading
/ in front of
page to make a domain relative path
var url = "/page/" + 1;