window.location.href appending instead of replacing

BforBerry

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.

WalksAway

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;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

why window.location appending instead of replacing the URL in ie

From Dev

window.location.href with POST instead of GET (or equivalent effect)

From Dev

Ruby replacing entire file instead of appending to it

From Dev

Why is this sed command replacing line instead of appending to it?

From Dev

Why is this sed command replacing line instead of appending to it?

From Dev

String is not a function on window location href

From Dev

window.location.href not working

From Dev

Make window.location.href load page from Server instead from browser cache

From Dev

Make window.location.href load page from Server instead from browser cache

From Dev

Enable and disable window.location.href

From Dev

onclick window.location.href with input value

From Dev

Javascript window.location.href onClick not working

From Dev

Passing parameter through "window.location.href"

From Dev

onchange event on window.location.href

From Dev

Window.location.href infinite loop

From Dev

Stubbing window.location.href with Sinon

From Dev

window.location.href not working in IE 11

From Dev

Using a variable for if window.location.href.indexOf()

From Dev

$window.location.href NOT WORKING in AngularJS

From Dev

JavaScript window.location.href not working in localhost

From Dev

window.location.href is not redirecting html page

From Dev

window.location.href not working on IE

From Dev

How to Pass QueryString in window.location.href?

From Dev

onclick window.location.href with input value

From Dev

window.location.href - help redirecting

From Dev

Use of window.location.href in crosswalk

From Dev

window.location.href and hash are not working on chrome

From Dev

Window.location.href infinite loop

From Dev

Javascript window.location.href onClick not working

Related Related

  1. 1

    why window.location appending instead of replacing the URL in ie

  2. 2

    window.location.href with POST instead of GET (or equivalent effect)

  3. 3

    Ruby replacing entire file instead of appending to it

  4. 4

    Why is this sed command replacing line instead of appending to it?

  5. 5

    Why is this sed command replacing line instead of appending to it?

  6. 6

    String is not a function on window location href

  7. 7

    window.location.href not working

  8. 8

    Make window.location.href load page from Server instead from browser cache

  9. 9

    Make window.location.href load page from Server instead from browser cache

  10. 10

    Enable and disable window.location.href

  11. 11

    onclick window.location.href with input value

  12. 12

    Javascript window.location.href onClick not working

  13. 13

    Passing parameter through "window.location.href"

  14. 14

    onchange event on window.location.href

  15. 15

    Window.location.href infinite loop

  16. 16

    Stubbing window.location.href with Sinon

  17. 17

    window.location.href not working in IE 11

  18. 18

    Using a variable for if window.location.href.indexOf()

  19. 19

    $window.location.href NOT WORKING in AngularJS

  20. 20

    JavaScript window.location.href not working in localhost

  21. 21

    window.location.href is not redirecting html page

  22. 22

    window.location.href not working on IE

  23. 23

    How to Pass QueryString in window.location.href?

  24. 24

    onclick window.location.href with input value

  25. 25

    window.location.href - help redirecting

  26. 26

    Use of window.location.href in crosswalk

  27. 27

    window.location.href and hash are not working on chrome

  28. 28

    Window.location.href infinite loop

  29. 29

    Javascript window.location.href onClick not working

HotTag

Archive