When the user returns the page, any visible form data is saved, and any JavaScript variables are reset. I say βvisibleβ form data, since hidden fields do not seem to be saved, but invisible inputs.
You can take advantage of this to determine if the page was a bootstrap or has already been loaded previously, for example, by clicking the back button.
Create an invisible input field (and not a type of "hidden") with the value "0" and inside the DOM ready to check to see if the value is set to "1"; if he knows that the page is already loaded, for example, using the "Back" button; if it is still "0", the page first loads, the value is set to "1".
Note. This is a little delicate in how it works, and probably does not work in all browsers; I built it with Chrome in mind.
- DOM must be loaded; Not all ready-made functions work. The one below is also jQuery ready; however (function () {}) is not in my instance.
- Input cannot be of type = "hidden". Set style = "display: none;" at the entrance.
.
<input id="backbuttonstate" type="text" value="0" style="display:none;" /> <script> document.addEventListener('DOMContentLoaded', function () { var ibackbutton = document.getElementById("backbuttonstate"); if (ibackbutton.value == "0") { </script>
source share