Keep Javascript changes when going back and forward in browser












0















I have the following shorted code to easier demonstrate my problem:



<script>
function change() {
document.getElementById('change').innerHTML = "Done!";
}
</script>

<div id="change" onclick="change();">Change me!</div>


Let's say the files name is test.html.



When clicking on Change me! the string will get changed to Done!.



Safari, Firefox: When clicking on the browser back button and then the browser forward button to go back to my test.html, it's showing Done!. This is what I expect!



Doing the same with other browsers (Chrome, Vivaldi, Min, Opera), I'm getting Change me! instead of Done!.



Safari and Firefox seem to keep the Javascript changes while other browsers load the page without the Javascript changes, when using back and forward buttons.



Is there a way to reach the same behavior for other browser as it's working in Safari and Firefox?










share|improve this question


















  • 2





    Why do you want to persist changes, if you trying to manage state of the page or an app try some different and robust approach

    – Ambrish Pathak
    Nov 27 '18 at 11:19











  • Similar to this question - stackoverflow.com/questions/31837195/…

    – Mortz
    Nov 27 '18 at 11:24











  • Try to explain why you need this feature on your code. Maybe you need a single page application? We don't know, try to share your needs

    – keul
    Nov 27 '18 at 11:25











  • @AmbrishPathak I have an iOS app that uses a WebView to display the content. There's also a button to add the actual page to the favorites. When clicking on this button it changes to Is in your favorites. When a user goes back and the forward to this page again he'll think it's not in his favorites because it's saying Is NOT in your favorites now.

    – David
    Nov 27 '18 at 11:27






  • 1





    You can look at using Window.sessionStorage or Window.localStorage of your browser to store and retrieve the state for the cases where back button/forward buttons are used

    – Mortz
    Nov 27 '18 at 11:37
















0















I have the following shorted code to easier demonstrate my problem:



<script>
function change() {
document.getElementById('change').innerHTML = "Done!";
}
</script>

<div id="change" onclick="change();">Change me!</div>


Let's say the files name is test.html.



When clicking on Change me! the string will get changed to Done!.



Safari, Firefox: When clicking on the browser back button and then the browser forward button to go back to my test.html, it's showing Done!. This is what I expect!



Doing the same with other browsers (Chrome, Vivaldi, Min, Opera), I'm getting Change me! instead of Done!.



Safari and Firefox seem to keep the Javascript changes while other browsers load the page without the Javascript changes, when using back and forward buttons.



Is there a way to reach the same behavior for other browser as it's working in Safari and Firefox?










share|improve this question


















  • 2





    Why do you want to persist changes, if you trying to manage state of the page or an app try some different and robust approach

    – Ambrish Pathak
    Nov 27 '18 at 11:19











  • Similar to this question - stackoverflow.com/questions/31837195/…

    – Mortz
    Nov 27 '18 at 11:24











  • Try to explain why you need this feature on your code. Maybe you need a single page application? We don't know, try to share your needs

    – keul
    Nov 27 '18 at 11:25











  • @AmbrishPathak I have an iOS app that uses a WebView to display the content. There's also a button to add the actual page to the favorites. When clicking on this button it changes to Is in your favorites. When a user goes back and the forward to this page again he'll think it's not in his favorites because it's saying Is NOT in your favorites now.

    – David
    Nov 27 '18 at 11:27






  • 1





    You can look at using Window.sessionStorage or Window.localStorage of your browser to store and retrieve the state for the cases where back button/forward buttons are used

    – Mortz
    Nov 27 '18 at 11:37














0












0








0








I have the following shorted code to easier demonstrate my problem:



<script>
function change() {
document.getElementById('change').innerHTML = "Done!";
}
</script>

<div id="change" onclick="change();">Change me!</div>


Let's say the files name is test.html.



When clicking on Change me! the string will get changed to Done!.



Safari, Firefox: When clicking on the browser back button and then the browser forward button to go back to my test.html, it's showing Done!. This is what I expect!



Doing the same with other browsers (Chrome, Vivaldi, Min, Opera), I'm getting Change me! instead of Done!.



Safari and Firefox seem to keep the Javascript changes while other browsers load the page without the Javascript changes, when using back and forward buttons.



Is there a way to reach the same behavior for other browser as it's working in Safari and Firefox?










share|improve this question














I have the following shorted code to easier demonstrate my problem:



<script>
function change() {
document.getElementById('change').innerHTML = "Done!";
}
</script>

<div id="change" onclick="change();">Change me!</div>


Let's say the files name is test.html.



When clicking on Change me! the string will get changed to Done!.



Safari, Firefox: When clicking on the browser back button and then the browser forward button to go back to my test.html, it's showing Done!. This is what I expect!



Doing the same with other browsers (Chrome, Vivaldi, Min, Opera), I'm getting Change me! instead of Done!.



Safari and Firefox seem to keep the Javascript changes while other browsers load the page without the Javascript changes, when using back and forward buttons.



Is there a way to reach the same behavior for other browser as it's working in Safari and Firefox?







javascript html dom browser back-button






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 27 '18 at 11:15









DavidDavid

6071215




6071215








  • 2





    Why do you want to persist changes, if you trying to manage state of the page or an app try some different and robust approach

    – Ambrish Pathak
    Nov 27 '18 at 11:19











  • Similar to this question - stackoverflow.com/questions/31837195/…

    – Mortz
    Nov 27 '18 at 11:24











  • Try to explain why you need this feature on your code. Maybe you need a single page application? We don't know, try to share your needs

    – keul
    Nov 27 '18 at 11:25











  • @AmbrishPathak I have an iOS app that uses a WebView to display the content. There's also a button to add the actual page to the favorites. When clicking on this button it changes to Is in your favorites. When a user goes back and the forward to this page again he'll think it's not in his favorites because it's saying Is NOT in your favorites now.

    – David
    Nov 27 '18 at 11:27






  • 1





    You can look at using Window.sessionStorage or Window.localStorage of your browser to store and retrieve the state for the cases where back button/forward buttons are used

    – Mortz
    Nov 27 '18 at 11:37














  • 2





    Why do you want to persist changes, if you trying to manage state of the page or an app try some different and robust approach

    – Ambrish Pathak
    Nov 27 '18 at 11:19











  • Similar to this question - stackoverflow.com/questions/31837195/…

    – Mortz
    Nov 27 '18 at 11:24











  • Try to explain why you need this feature on your code. Maybe you need a single page application? We don't know, try to share your needs

    – keul
    Nov 27 '18 at 11:25











  • @AmbrishPathak I have an iOS app that uses a WebView to display the content. There's also a button to add the actual page to the favorites. When clicking on this button it changes to Is in your favorites. When a user goes back and the forward to this page again he'll think it's not in his favorites because it's saying Is NOT in your favorites now.

    – David
    Nov 27 '18 at 11:27






  • 1





    You can look at using Window.sessionStorage or Window.localStorage of your browser to store and retrieve the state for the cases where back button/forward buttons are used

    – Mortz
    Nov 27 '18 at 11:37








2




2





Why do you want to persist changes, if you trying to manage state of the page or an app try some different and robust approach

– Ambrish Pathak
Nov 27 '18 at 11:19





Why do you want to persist changes, if you trying to manage state of the page or an app try some different and robust approach

– Ambrish Pathak
Nov 27 '18 at 11:19













Similar to this question - stackoverflow.com/questions/31837195/…

– Mortz
Nov 27 '18 at 11:24





Similar to this question - stackoverflow.com/questions/31837195/…

– Mortz
Nov 27 '18 at 11:24













Try to explain why you need this feature on your code. Maybe you need a single page application? We don't know, try to share your needs

– keul
Nov 27 '18 at 11:25





Try to explain why you need this feature on your code. Maybe you need a single page application? We don't know, try to share your needs

– keul
Nov 27 '18 at 11:25













@AmbrishPathak I have an iOS app that uses a WebView to display the content. There's also a button to add the actual page to the favorites. When clicking on this button it changes to Is in your favorites. When a user goes back and the forward to this page again he'll think it's not in his favorites because it's saying Is NOT in your favorites now.

– David
Nov 27 '18 at 11:27





@AmbrishPathak I have an iOS app that uses a WebView to display the content. There's also a button to add the actual page to the favorites. When clicking on this button it changes to Is in your favorites. When a user goes back and the forward to this page again he'll think it's not in his favorites because it's saying Is NOT in your favorites now.

– David
Nov 27 '18 at 11:27




1




1





You can look at using Window.sessionStorage or Window.localStorage of your browser to store and retrieve the state for the cases where back button/forward buttons are used

– Mortz
Nov 27 '18 at 11:37





You can look at using Window.sessionStorage or Window.localStorage of your browser to store and retrieve the state for the cases where back button/forward buttons are used

– Mortz
Nov 27 '18 at 11:37












1 Answer
1






active

oldest

votes


















1














You can look at using Window.sessionStorage or Window.localStorage of your browser to store and retrieve the state for the cases where back button/forward buttons are used



<script>
window.addEventListener("load", function(event) {
setHTML();
})

function setHTML() {
document.getElementById('change').innerHTML = window.sessionStorage.getItem("state")||"NOT IN FAVORITES";
}

function addToFavorites() {
window.sessionStorage.setItem("state", "Done");
document.getElementById('change').innerHTML = window.sessionStorage.getItem("state");

}
</script>

<div id="change" onclick="addToFavorites();">Change me!</div>





share|improve this answer


























  • You need to change onclick="change();" to onclick="addToFavorites();". Made the edit in the answer.

    – Mortz
    Nov 27 '18 at 12:28











  • Your code is working now but it's still the same issue when going back and forward.

    – David
    Nov 27 '18 at 12:38











  • Seems to be working for me - you can try to run window.sessionStorage.getItem("state") in the console before and after clicking to see if it is being stored. Also, you can probably put in a console.log in the load event listener to see if the re-load is being registered on clicking back and forward buttons.

    – Mortz
    Nov 27 '18 at 12:52











  • Can you please check if it's working with the code in your answer? You and I changed it several times and I'm not sure if it's the code that is working for you. Maybe you have an older version?!

    – David
    Nov 28 '18 at 11:42











  • There was a missing ) in the window.addEventListener. Have updated - should work now.

    – Mortz
    Nov 28 '18 at 12:06











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%2f53498453%2fkeep-javascript-changes-when-going-back-and-forward-in-browser%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














You can look at using Window.sessionStorage or Window.localStorage of your browser to store and retrieve the state for the cases where back button/forward buttons are used



<script>
window.addEventListener("load", function(event) {
setHTML();
})

function setHTML() {
document.getElementById('change').innerHTML = window.sessionStorage.getItem("state")||"NOT IN FAVORITES";
}

function addToFavorites() {
window.sessionStorage.setItem("state", "Done");
document.getElementById('change').innerHTML = window.sessionStorage.getItem("state");

}
</script>

<div id="change" onclick="addToFavorites();">Change me!</div>





share|improve this answer


























  • You need to change onclick="change();" to onclick="addToFavorites();". Made the edit in the answer.

    – Mortz
    Nov 27 '18 at 12:28











  • Your code is working now but it's still the same issue when going back and forward.

    – David
    Nov 27 '18 at 12:38











  • Seems to be working for me - you can try to run window.sessionStorage.getItem("state") in the console before and after clicking to see if it is being stored. Also, you can probably put in a console.log in the load event listener to see if the re-load is being registered on clicking back and forward buttons.

    – Mortz
    Nov 27 '18 at 12:52











  • Can you please check if it's working with the code in your answer? You and I changed it several times and I'm not sure if it's the code that is working for you. Maybe you have an older version?!

    – David
    Nov 28 '18 at 11:42











  • There was a missing ) in the window.addEventListener. Have updated - should work now.

    – Mortz
    Nov 28 '18 at 12:06
















1














You can look at using Window.sessionStorage or Window.localStorage of your browser to store and retrieve the state for the cases where back button/forward buttons are used



<script>
window.addEventListener("load", function(event) {
setHTML();
})

function setHTML() {
document.getElementById('change').innerHTML = window.sessionStorage.getItem("state")||"NOT IN FAVORITES";
}

function addToFavorites() {
window.sessionStorage.setItem("state", "Done");
document.getElementById('change').innerHTML = window.sessionStorage.getItem("state");

}
</script>

<div id="change" onclick="addToFavorites();">Change me!</div>





share|improve this answer


























  • You need to change onclick="change();" to onclick="addToFavorites();". Made the edit in the answer.

    – Mortz
    Nov 27 '18 at 12:28











  • Your code is working now but it's still the same issue when going back and forward.

    – David
    Nov 27 '18 at 12:38











  • Seems to be working for me - you can try to run window.sessionStorage.getItem("state") in the console before and after clicking to see if it is being stored. Also, you can probably put in a console.log in the load event listener to see if the re-load is being registered on clicking back and forward buttons.

    – Mortz
    Nov 27 '18 at 12:52











  • Can you please check if it's working with the code in your answer? You and I changed it several times and I'm not sure if it's the code that is working for you. Maybe you have an older version?!

    – David
    Nov 28 '18 at 11:42











  • There was a missing ) in the window.addEventListener. Have updated - should work now.

    – Mortz
    Nov 28 '18 at 12:06














1












1








1







You can look at using Window.sessionStorage or Window.localStorage of your browser to store and retrieve the state for the cases where back button/forward buttons are used



<script>
window.addEventListener("load", function(event) {
setHTML();
})

function setHTML() {
document.getElementById('change').innerHTML = window.sessionStorage.getItem("state")||"NOT IN FAVORITES";
}

function addToFavorites() {
window.sessionStorage.setItem("state", "Done");
document.getElementById('change').innerHTML = window.sessionStorage.getItem("state");

}
</script>

<div id="change" onclick="addToFavorites();">Change me!</div>





share|improve this answer















You can look at using Window.sessionStorage or Window.localStorage of your browser to store and retrieve the state for the cases where back button/forward buttons are used



<script>
window.addEventListener("load", function(event) {
setHTML();
})

function setHTML() {
document.getElementById('change').innerHTML = window.sessionStorage.getItem("state")||"NOT IN FAVORITES";
}

function addToFavorites() {
window.sessionStorage.setItem("state", "Done");
document.getElementById('change').innerHTML = window.sessionStorage.getItem("state");

}
</script>

<div id="change" onclick="addToFavorites();">Change me!</div>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 28 '18 at 12:05

























answered Nov 27 '18 at 11:51









MortzMortz

737418




737418













  • You need to change onclick="change();" to onclick="addToFavorites();". Made the edit in the answer.

    – Mortz
    Nov 27 '18 at 12:28











  • Your code is working now but it's still the same issue when going back and forward.

    – David
    Nov 27 '18 at 12:38











  • Seems to be working for me - you can try to run window.sessionStorage.getItem("state") in the console before and after clicking to see if it is being stored. Also, you can probably put in a console.log in the load event listener to see if the re-load is being registered on clicking back and forward buttons.

    – Mortz
    Nov 27 '18 at 12:52











  • Can you please check if it's working with the code in your answer? You and I changed it several times and I'm not sure if it's the code that is working for you. Maybe you have an older version?!

    – David
    Nov 28 '18 at 11:42











  • There was a missing ) in the window.addEventListener. Have updated - should work now.

    – Mortz
    Nov 28 '18 at 12:06



















  • You need to change onclick="change();" to onclick="addToFavorites();". Made the edit in the answer.

    – Mortz
    Nov 27 '18 at 12:28











  • Your code is working now but it's still the same issue when going back and forward.

    – David
    Nov 27 '18 at 12:38











  • Seems to be working for me - you can try to run window.sessionStorage.getItem("state") in the console before and after clicking to see if it is being stored. Also, you can probably put in a console.log in the load event listener to see if the re-load is being registered on clicking back and forward buttons.

    – Mortz
    Nov 27 '18 at 12:52











  • Can you please check if it's working with the code in your answer? You and I changed it several times and I'm not sure if it's the code that is working for you. Maybe you have an older version?!

    – David
    Nov 28 '18 at 11:42











  • There was a missing ) in the window.addEventListener. Have updated - should work now.

    – Mortz
    Nov 28 '18 at 12:06

















You need to change onclick="change();" to onclick="addToFavorites();". Made the edit in the answer.

– Mortz
Nov 27 '18 at 12:28





You need to change onclick="change();" to onclick="addToFavorites();". Made the edit in the answer.

– Mortz
Nov 27 '18 at 12:28













Your code is working now but it's still the same issue when going back and forward.

– David
Nov 27 '18 at 12:38





Your code is working now but it's still the same issue when going back and forward.

– David
Nov 27 '18 at 12:38













Seems to be working for me - you can try to run window.sessionStorage.getItem("state") in the console before and after clicking to see if it is being stored. Also, you can probably put in a console.log in the load event listener to see if the re-load is being registered on clicking back and forward buttons.

– Mortz
Nov 27 '18 at 12:52





Seems to be working for me - you can try to run window.sessionStorage.getItem("state") in the console before and after clicking to see if it is being stored. Also, you can probably put in a console.log in the load event listener to see if the re-load is being registered on clicking back and forward buttons.

– Mortz
Nov 27 '18 at 12:52













Can you please check if it's working with the code in your answer? You and I changed it several times and I'm not sure if it's the code that is working for you. Maybe you have an older version?!

– David
Nov 28 '18 at 11:42





Can you please check if it's working with the code in your answer? You and I changed it several times and I'm not sure if it's the code that is working for you. Maybe you have an older version?!

– David
Nov 28 '18 at 11:42













There was a missing ) in the window.addEventListener. Have updated - should work now.

– Mortz
Nov 28 '18 at 12:06





There was a missing ) in the window.addEventListener. Have updated - should work now.

– Mortz
Nov 28 '18 at 12:06




















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53498453%2fkeep-javascript-changes-when-going-back-and-forward-in-browser%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

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks