Problem with history.pushState when using AJAX












1














I'm quite new to javascript and history API in particular.



I'm making an AJAX GET request to load next or previous page on a website.



function ajax_get_update(url)
{
$.ajax({
async: true,
type: "GET",
url: url,
cache: true,
success: function(result){
sessionStorage.setItem("result", result);
update_content(result);
}
});
history.pushState(url, url, url)
}


AJAX part and changing page content works perfectly fine. As you can see I'm using history.pushState(url, url, url) to add next page to history so I could use back button to get back to that page.



url looks something like this http://127.0.0.1:8000/?page=2



The problem is if I'd go on page3 from page2 and then to page3 again and so on, history then would contain all these transitions, and when I click on the back button I keep altering back and forth between page2 and page3 few times before I get to the page1 eventually. What I want is page3 -> page2 -> page1 and so on.



How would you solve this problem?










share|improve this question




















  • 1




    That is how history works :) You could try to move your history.pushState() into the success handler and update the history only on certain cases. I recommend also to update your history only if the ajax request was successful otherwise your application may have an invalid sate.
    – gearsdigital
    Nov 20 at 20:42












  • @gearsdigital Good call. Any ideas on how to implement this behaviour page3 -> page2 -> page1 on a back button? Instead of page3->page2->page3->page2...
    – bloodwithmilk
    Nov 20 at 20:54












  • Do you have any information about the current page? Other than the url?
    – gearsdigital
    Nov 20 at 20:55












  • @gearsdigital not realy... content is unique though
    – bloodwithmilk
    Nov 20 at 20:58










  • But parameter is always ?page?
    – gearsdigital
    Nov 20 at 20:58
















1














I'm quite new to javascript and history API in particular.



I'm making an AJAX GET request to load next or previous page on a website.



function ajax_get_update(url)
{
$.ajax({
async: true,
type: "GET",
url: url,
cache: true,
success: function(result){
sessionStorage.setItem("result", result);
update_content(result);
}
});
history.pushState(url, url, url)
}


AJAX part and changing page content works perfectly fine. As you can see I'm using history.pushState(url, url, url) to add next page to history so I could use back button to get back to that page.



url looks something like this http://127.0.0.1:8000/?page=2



The problem is if I'd go on page3 from page2 and then to page3 again and so on, history then would contain all these transitions, and when I click on the back button I keep altering back and forth between page2 and page3 few times before I get to the page1 eventually. What I want is page3 -> page2 -> page1 and so on.



How would you solve this problem?










share|improve this question




















  • 1




    That is how history works :) You could try to move your history.pushState() into the success handler and update the history only on certain cases. I recommend also to update your history only if the ajax request was successful otherwise your application may have an invalid sate.
    – gearsdigital
    Nov 20 at 20:42












  • @gearsdigital Good call. Any ideas on how to implement this behaviour page3 -> page2 -> page1 on a back button? Instead of page3->page2->page3->page2...
    – bloodwithmilk
    Nov 20 at 20:54












  • Do you have any information about the current page? Other than the url?
    – gearsdigital
    Nov 20 at 20:55












  • @gearsdigital not realy... content is unique though
    – bloodwithmilk
    Nov 20 at 20:58










  • But parameter is always ?page?
    – gearsdigital
    Nov 20 at 20:58














1












1








1







I'm quite new to javascript and history API in particular.



I'm making an AJAX GET request to load next or previous page on a website.



function ajax_get_update(url)
{
$.ajax({
async: true,
type: "GET",
url: url,
cache: true,
success: function(result){
sessionStorage.setItem("result", result);
update_content(result);
}
});
history.pushState(url, url, url)
}


AJAX part and changing page content works perfectly fine. As you can see I'm using history.pushState(url, url, url) to add next page to history so I could use back button to get back to that page.



url looks something like this http://127.0.0.1:8000/?page=2



The problem is if I'd go on page3 from page2 and then to page3 again and so on, history then would contain all these transitions, and when I click on the back button I keep altering back and forth between page2 and page3 few times before I get to the page1 eventually. What I want is page3 -> page2 -> page1 and so on.



How would you solve this problem?










share|improve this question















I'm quite new to javascript and history API in particular.



I'm making an AJAX GET request to load next or previous page on a website.



function ajax_get_update(url)
{
$.ajax({
async: true,
type: "GET",
url: url,
cache: true,
success: function(result){
sessionStorage.setItem("result", result);
update_content(result);
}
});
history.pushState(url, url, url)
}


AJAX part and changing page content works perfectly fine. As you can see I'm using history.pushState(url, url, url) to add next page to history so I could use back button to get back to that page.



url looks something like this http://127.0.0.1:8000/?page=2



The problem is if I'd go on page3 from page2 and then to page3 again and so on, history then would contain all these transitions, and when I click on the back button I keep altering back and forth between page2 and page3 few times before I get to the page1 eventually. What I want is page3 -> page2 -> page1 and so on.



How would you solve this problem?







javascript url url-rewriting html5-history






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 20:59

























asked Nov 20 at 20:35









bloodwithmilk

177112




177112








  • 1




    That is how history works :) You could try to move your history.pushState() into the success handler and update the history only on certain cases. I recommend also to update your history only if the ajax request was successful otherwise your application may have an invalid sate.
    – gearsdigital
    Nov 20 at 20:42












  • @gearsdigital Good call. Any ideas on how to implement this behaviour page3 -> page2 -> page1 on a back button? Instead of page3->page2->page3->page2...
    – bloodwithmilk
    Nov 20 at 20:54












  • Do you have any information about the current page? Other than the url?
    – gearsdigital
    Nov 20 at 20:55












  • @gearsdigital not realy... content is unique though
    – bloodwithmilk
    Nov 20 at 20:58










  • But parameter is always ?page?
    – gearsdigital
    Nov 20 at 20:58














  • 1




    That is how history works :) You could try to move your history.pushState() into the success handler and update the history only on certain cases. I recommend also to update your history only if the ajax request was successful otherwise your application may have an invalid sate.
    – gearsdigital
    Nov 20 at 20:42












  • @gearsdigital Good call. Any ideas on how to implement this behaviour page3 -> page2 -> page1 on a back button? Instead of page3->page2->page3->page2...
    – bloodwithmilk
    Nov 20 at 20:54












  • Do you have any information about the current page? Other than the url?
    – gearsdigital
    Nov 20 at 20:55












  • @gearsdigital not realy... content is unique though
    – bloodwithmilk
    Nov 20 at 20:58










  • But parameter is always ?page?
    – gearsdigital
    Nov 20 at 20:58








1




1




That is how history works :) You could try to move your history.pushState() into the success handler and update the history only on certain cases. I recommend also to update your history only if the ajax request was successful otherwise your application may have an invalid sate.
– gearsdigital
Nov 20 at 20:42






That is how history works :) You could try to move your history.pushState() into the success handler and update the history only on certain cases. I recommend also to update your history only if the ajax request was successful otherwise your application may have an invalid sate.
– gearsdigital
Nov 20 at 20:42














@gearsdigital Good call. Any ideas on how to implement this behaviour page3 -> page2 -> page1 on a back button? Instead of page3->page2->page3->page2...
– bloodwithmilk
Nov 20 at 20:54






@gearsdigital Good call. Any ideas on how to implement this behaviour page3 -> page2 -> page1 on a back button? Instead of page3->page2->page3->page2...
– bloodwithmilk
Nov 20 at 20:54














Do you have any information about the current page? Other than the url?
– gearsdigital
Nov 20 at 20:55






Do you have any information about the current page? Other than the url?
– gearsdigital
Nov 20 at 20:55














@gearsdigital not realy... content is unique though
– bloodwithmilk
Nov 20 at 20:58




@gearsdigital not realy... content is unique though
– bloodwithmilk
Nov 20 at 20:58












But parameter is always ?page?
– gearsdigital
Nov 20 at 20:58




But parameter is always ?page?
– gearsdigital
Nov 20 at 20:58












1 Answer
1






active

oldest

votes


















1














This is untested but should fit your needs. The idea here is to update the window.history only for newly added urls.



On every successful request we're checking if the current url is present in myState. If not we can assume a new page and update the history and putting the url to our private cache myState.



This will keep your urls in order and prevent duplicated history entries.



Please be aware that this code is using ES2015. You can search the web for polyfills :)



// it is probably not a good idea to keep your own history
let myState = ;

function ajax_get_update(url) {
$.ajax({
async: true,
type: "GET",
url: url,
cache: true,
success: function(result) {
sessionStorage.setItem("result", result);
update_content(result);
update_history(url);
}
});
}

function update_history(url) {
// window.history is only updated when the current url is not
// saved in our private history
if (!myState.includes(url)) {
history.pushState(url, url, url);
myState.push(url);
}
}





share|improve this answer



















  • 1




    developer.mozilla.org/de/docs/Web/JavaScript/Reference/…
    – gearsdigital
    Nov 20 at 21:37






  • 1




    (!state.includes(url)) must be (!myState.includes(url)), right?
    – bloodwithmilk
    Nov 20 at 21:37










  • Sure! My bad :) But you got the idea, right?
    – gearsdigital
    Nov 20 at 21:38












  • Works perfectly fine. Thank you so much for your time and patience.
    – bloodwithmilk
    Nov 20 at 21:41












  • Nah, no problem! Glad it worked for you!
    – gearsdigital
    Nov 20 at 21:43











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401109%2fproblem-with-history-pushstate-when-using-ajax%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














This is untested but should fit your needs. The idea here is to update the window.history only for newly added urls.



On every successful request we're checking if the current url is present in myState. If not we can assume a new page and update the history and putting the url to our private cache myState.



This will keep your urls in order and prevent duplicated history entries.



Please be aware that this code is using ES2015. You can search the web for polyfills :)



// it is probably not a good idea to keep your own history
let myState = ;

function ajax_get_update(url) {
$.ajax({
async: true,
type: "GET",
url: url,
cache: true,
success: function(result) {
sessionStorage.setItem("result", result);
update_content(result);
update_history(url);
}
});
}

function update_history(url) {
// window.history is only updated when the current url is not
// saved in our private history
if (!myState.includes(url)) {
history.pushState(url, url, url);
myState.push(url);
}
}





share|improve this answer



















  • 1




    developer.mozilla.org/de/docs/Web/JavaScript/Reference/…
    – gearsdigital
    Nov 20 at 21:37






  • 1




    (!state.includes(url)) must be (!myState.includes(url)), right?
    – bloodwithmilk
    Nov 20 at 21:37










  • Sure! My bad :) But you got the idea, right?
    – gearsdigital
    Nov 20 at 21:38












  • Works perfectly fine. Thank you so much for your time and patience.
    – bloodwithmilk
    Nov 20 at 21:41












  • Nah, no problem! Glad it worked for you!
    – gearsdigital
    Nov 20 at 21:43
















1














This is untested but should fit your needs. The idea here is to update the window.history only for newly added urls.



On every successful request we're checking if the current url is present in myState. If not we can assume a new page and update the history and putting the url to our private cache myState.



This will keep your urls in order and prevent duplicated history entries.



Please be aware that this code is using ES2015. You can search the web for polyfills :)



// it is probably not a good idea to keep your own history
let myState = ;

function ajax_get_update(url) {
$.ajax({
async: true,
type: "GET",
url: url,
cache: true,
success: function(result) {
sessionStorage.setItem("result", result);
update_content(result);
update_history(url);
}
});
}

function update_history(url) {
// window.history is only updated when the current url is not
// saved in our private history
if (!myState.includes(url)) {
history.pushState(url, url, url);
myState.push(url);
}
}





share|improve this answer



















  • 1




    developer.mozilla.org/de/docs/Web/JavaScript/Reference/…
    – gearsdigital
    Nov 20 at 21:37






  • 1




    (!state.includes(url)) must be (!myState.includes(url)), right?
    – bloodwithmilk
    Nov 20 at 21:37










  • Sure! My bad :) But you got the idea, right?
    – gearsdigital
    Nov 20 at 21:38












  • Works perfectly fine. Thank you so much for your time and patience.
    – bloodwithmilk
    Nov 20 at 21:41












  • Nah, no problem! Glad it worked for you!
    – gearsdigital
    Nov 20 at 21:43














1












1








1






This is untested but should fit your needs. The idea here is to update the window.history only for newly added urls.



On every successful request we're checking if the current url is present in myState. If not we can assume a new page and update the history and putting the url to our private cache myState.



This will keep your urls in order and prevent duplicated history entries.



Please be aware that this code is using ES2015. You can search the web for polyfills :)



// it is probably not a good idea to keep your own history
let myState = ;

function ajax_get_update(url) {
$.ajax({
async: true,
type: "GET",
url: url,
cache: true,
success: function(result) {
sessionStorage.setItem("result", result);
update_content(result);
update_history(url);
}
});
}

function update_history(url) {
// window.history is only updated when the current url is not
// saved in our private history
if (!myState.includes(url)) {
history.pushState(url, url, url);
myState.push(url);
}
}





share|improve this answer














This is untested but should fit your needs. The idea here is to update the window.history only for newly added urls.



On every successful request we're checking if the current url is present in myState. If not we can assume a new page and update the history and putting the url to our private cache myState.



This will keep your urls in order and prevent duplicated history entries.



Please be aware that this code is using ES2015. You can search the web for polyfills :)



// it is probably not a good idea to keep your own history
let myState = ;

function ajax_get_update(url) {
$.ajax({
async: true,
type: "GET",
url: url,
cache: true,
success: function(result) {
sessionStorage.setItem("result", result);
update_content(result);
update_history(url);
}
});
}

function update_history(url) {
// window.history is only updated when the current url is not
// saved in our private history
if (!myState.includes(url)) {
history.pushState(url, url, url);
myState.push(url);
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 21:38

























answered Nov 20 at 21:33









gearsdigital

9,27362867




9,27362867








  • 1




    developer.mozilla.org/de/docs/Web/JavaScript/Reference/…
    – gearsdigital
    Nov 20 at 21:37






  • 1




    (!state.includes(url)) must be (!myState.includes(url)), right?
    – bloodwithmilk
    Nov 20 at 21:37










  • Sure! My bad :) But you got the idea, right?
    – gearsdigital
    Nov 20 at 21:38












  • Works perfectly fine. Thank you so much for your time and patience.
    – bloodwithmilk
    Nov 20 at 21:41












  • Nah, no problem! Glad it worked for you!
    – gearsdigital
    Nov 20 at 21:43














  • 1




    developer.mozilla.org/de/docs/Web/JavaScript/Reference/…
    – gearsdigital
    Nov 20 at 21:37






  • 1




    (!state.includes(url)) must be (!myState.includes(url)), right?
    – bloodwithmilk
    Nov 20 at 21:37










  • Sure! My bad :) But you got the idea, right?
    – gearsdigital
    Nov 20 at 21:38












  • Works perfectly fine. Thank you so much for your time and patience.
    – bloodwithmilk
    Nov 20 at 21:41












  • Nah, no problem! Glad it worked for you!
    – gearsdigital
    Nov 20 at 21:43








1




1




developer.mozilla.org/de/docs/Web/JavaScript/Reference/…
– gearsdigital
Nov 20 at 21:37




developer.mozilla.org/de/docs/Web/JavaScript/Reference/…
– gearsdigital
Nov 20 at 21:37




1




1




(!state.includes(url)) must be (!myState.includes(url)), right?
– bloodwithmilk
Nov 20 at 21:37




(!state.includes(url)) must be (!myState.includes(url)), right?
– bloodwithmilk
Nov 20 at 21:37












Sure! My bad :) But you got the idea, right?
– gearsdigital
Nov 20 at 21:38






Sure! My bad :) But you got the idea, right?
– gearsdigital
Nov 20 at 21:38














Works perfectly fine. Thank you so much for your time and patience.
– bloodwithmilk
Nov 20 at 21:41






Works perfectly fine. Thank you so much for your time and patience.
– bloodwithmilk
Nov 20 at 21:41














Nah, no problem! Glad it worked for you!
– gearsdigital
Nov 20 at 21:43




Nah, no problem! Glad it worked for you!
– gearsdigital
Nov 20 at 21:43


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


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





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


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




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401109%2fproblem-with-history-pushstate-when-using-ajax%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python