0

I need to synchronize a date returned from ASP.NET with a date returned from JavaScript. So, I send the date from the server in a hidden field, then get the new Date() from JavaScript and obtain the time offset. Only approximation of the time difference is needed.

All works well when the page first loads -- the server date stored in the hidden field and the value form JavaScript "match". But, when I navigate from the page and then click the Back button, the page now comes from browser cache, so the server value is outdated as compared to the JavaScript date.

Is there any way to make the two dates consistent -- both taken from cache or both not taken from cache? I guess I need a kind of client-side page modified date, which would not change if the page comes from the browser cache. I tried document.lastModified, but that changes when a page comes from cache.

Thank you.

user1044169
  • 2,686
  • 6
  • 35
  • 64
  • Note that some browsers have a bfcache that is used when pressing the back button: https://stackoverflow.com/questions/60382446/which-browsers-have-a-back-forward-cache-which-keeps-modifications-to-dom/60382721 – Flimm Feb 24 '20 at 19:52

1 Answers1

0

Why not just save the offset in a second hidden input?

When the page loads, have JavaScript check the value of the hidden "Offfset" input and populate it if it's empty (using your original hidden input as a reference). If the user navigates away and then clicks Back, the Offset field will still contain the calculated value and JavaScript won't try to update it.

@user1044169: Good catch; I think you are correct. The best advise seems to be to save the data in a cookie.

Community
  • 1
  • 1
mr_plum
  • 2,448
  • 1
  • 18
  • 31
  • Not sure this would work. If i understand it correctly, what's in the browser cache is a copy of the page as received from the server, without any DOM manipulations. So, if a user clicks Back button, the hidden Offset field will be emtpy, even through it previously had a value. – user1044169 Mar 16 '13 at 16:52