Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if Chrome restores the page from yesterday

I have a form with an date input field. If you load the page the input is filled with the current date. But if Chrome starts and restores the last opened tabs from yesterday, there is always the date from yesterday in the form. If I reload the page it shows the correct date. But I would like Chrome to show the current date right away.

Any clever idea how to fix this?

Update: Page is set to cache-control:no-cache, private.

like image 595
PiTheNumber Avatar asked Dec 11 '17 09:12

PiTheNumber


1 Answers

I placed a date field and a JavaScript on the page. Turns out the page is loaded from cache. There is still the date from yesterday in the form and the JavaScript is executed same way as on page load. If I compare the date I can detect the old page:

<input id="form_date" name="form_date" type="hidden" value="2017-12-19 17:52:08">
<script>
    // Day compare
    Date.prototype.sameDay = function(d) {
        return this.getFullYear() === d.getFullYear()
            && this.getDate() === d.getDate()
            && this.getMonth() === d.getMonth();
    };
    var x_today = new Date();
    var x_page_date = new Date($('#form_date').val());
    if (console && !x_today.sameDay( x_page_date )){
        console.log("Old page!");
        console.log('page date: ', x_page_date);
        console.log('today: ', x_today);
    }
</script>

From here you could reload the page or ask the user what to do.

like image 105
PiTheNumber Avatar answered Nov 12 '22 06:11

PiTheNumber