prevent history.replaceState appending to the url

prevent history.replaceState appending to the url



I am using window.history.replaceState and having some issues in that it keeps appending to the url.


window.history.replaceState



It has been marked as a duplicate of this questionwhich I think is a mistake as when I use that code the same issue is occurring and it keeps appending the index to the url as described below.



Please see my code below:


let index = 1;

function incrementIndexAndUpdateUrl()
index++;
window.history.replaceState('Object', 'Title', `$window.location.href /$index`);



The issue I am having is that the url keeps appending the number so it is doing something like the following:



https://slackoverflowz.com/questions/ask/2/3/4



Does anyone know how the code should look to update the url like as follows:



It is also worth noting the url is dynamic so I can't hardcode the path. I just want to update the index at the end of the url.






Have you tried removing the last numbers and then adding index?

– Luca Kiebel
Sep 17 '18 at 14:25






@LucaKiebel how do you mean?

– peter flanagan
Sep 17 '18 at 14:26






const index and index++? Also window.location.href will contain the numbers you've just appended

– George
Sep 17 '18 at 14:26


const index


index++


window.location.href






You should do it from window.location.origin and start appending the pathname

– Mohith
Sep 17 '18 at 14:26


window.location.origin


pathname






@George sorry, I just wrote the code here, it is actually in a. react component, there is no issue with const. You are correct though, that is the issue

– peter flanagan
Sep 17 '18 at 14:27


const




2 Answers
2



Since you are appending an /something, you are actually changing the pathname.
So the easy way, is to store the original one before you do change it:


/something




let index = 1;
const path = location.pathname;

btn.onclick = incrementIndexAndUpdateUrl;

function incrementIndexAndUpdateUrl()
index++;
window.history.replaceState('Object', 'Title', `$path/$index`);
console.log(location.href);


<button id="btn">increment</button>



And as a fiddle for Chrome.






makes sense! thanks @kaiido

– peter flanagan
Sep 17 '18 at 14:45



You should do it from window.location.origin and start appending the pathname, so probably your answer would be


let index = 1;

function incrementIndexAndUpdateUrl()
index++;
window.history.replaceState('Object', 'Title', `$window.location.origin/question/ask/$index`);






sorry @mohit the url is dynamic

– peter flanagan
Sep 17 '18 at 14:30



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)