JavaScript window.location.href appending instead of replacing window location href new tab,window location href relative,window

I have a little issue that I couldn't figure out. I have a slider and I want to change url when I click prev and 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 stackoverflow.com/page/1/page/2

But I just want to replace it stackoverflow.com/page/1 to stackoverflow.com/page/2

How can I fix it? Thank you for your help.

Answer:1

window.location.href returns the entire url...

if you just want to add the "/page/1"

use

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

https://developer.mozilla.org/en-US/docs/Web/API/Window/location

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;
Answer:2

All you want is a leading / in front of page to make a domain relative path

try

var url =  "/page/" + 1;
Answer:3

I have a bootstrap popover and its content is a kendo Ui color picker widget. When you click in the color picker widget, the popover close even if the trigger option is set to focus. Why? How to ...

I have a bootstrap popover and its content is a kendo Ui color picker widget. When you click in the color picker widget, the popover close even if the trigger option is set to focus. Why? How to ...

  1. bootstrap popover close button
  2. bootstrap popover not closing
  3. prevent bootstrap popover from closing
  4. closing bootstrap popover

I am using the Ionic framework to build an android app. I have a form as such: <form action='https://secure.payu.in/_payment' method="POST" target="..."> ...... </form> Is there a way ...

I am using the Ionic framework to build an android app. I have a form as such: <form action='https://secure.payu.in/_payment' method="POST" target="..."> ...... </form> Is there a way ...

  1. change formula
  2. change format
  3. change formula excel
  4. change formula to value
  5. change format of date in excel
  6. change format of picture
  7. change formatter vscode
  8. change formula google sheets
  9. change format of date in r
  10. change formatting in word
  11. change format of datetime python
  12. change format of date in sql
  13. change form template
  14. change format of outlook
  15. change format to pdf
  16. change form synonym
  17. change form real estate hawaii
  18. change format of pivot table
  19. change format sas
  20. change form action jquery

Beginning JavaScript learner here... I'm working through Adrian Neumann's Simple Programming Problems and my question is about number 5 in the elementary exercises. Write a program that asks the ...

Beginning JavaScript learner here... I'm working through Adrian Neumann's Simple Programming Problems and my question is about number 5 in the elementary exercises. Write a program that asks the ...

  1. compare contents array javascript
  2. javascript array contents equal
  3. javascript array contents
  4. javascript array contents to string
  5. javascript array contents disappear

I am trying to insert a simple img tag into the editor. jQuery('<img />').attr('src', '/uploads/images/' + res.data.filename); jQuery('.summernote').summernote('insertNode', $image[0]); I ...

I am trying to insert a simple img tag into the editor. jQuery('<img />').attr('src', '/uploads/images/' + res.data.filename); jQuery('.summernote').summernote('insertNode', $image[0]); I ...