I would like to execute a JavaScript function after a web-page was refreshed (any way: F5 key, browser's refresh key, user re-enter same URL or any other method). This method should be execute only after refresh, not after 1st page-load.
Something like:
window.onrefresh = function() { ... }; // of curse, onrefresh isn't real event
I find some solutions in the web, but none meet my needs:
Dirty Flag
This method is simple, on load check flag - if flag not exist this is first load, else set the flag:
- Save flag in local-storage/cookie. The flag can be current Date/Time. If on-load flag not exist create it - this is first load. Else if flag exist compare it to current Date/Time - after refresh.
- Save the flag in the site URL. The flag will be '#' sign ('#' - will not change the site Navigation). Same as before on-load test if '#' exist at the end of the URL this is "refresh" load, else this is a first load - we need to add a '#' at the end of the URL.
performance.navigation
The window object has this property: window.performance.navigation.type
which can have 3 values:
0 - Page loaded due to link
1 - Page Reloaded
2 - Page loaded due to Back button
However this not working as expected. This value is always "1", on both first load and after refresh.