If you want to detect an HTTP GET update, not just a POST, here is a hacking job that works mostly in modern browsers.
JavaScript:
window.onload = function () { // regex for finding "loaded" query string parameter var qsRegex = /^(\?|.+&)loaded=\d/ig; if (!qsRegex.test(location.search)) { var loc = window.location.href + (window.location.search.length ? '&' : '?') + 'loaded=1'; window.history.replaceState(null, document.title, loc); } };
WITH#:
public bool IsPageRefresh { get { return !string.IsNullOrEmpty(Request.QueryString["loaded"]); } }
When the page loads, it will change the QueryString loaded=1 parameter without reloading the page (again, this - window.history.replaceState - works only in post-archaic browsers ). Then, when the user refreshes the page, the server can check for the presence of the loaded parameter of the query string.
Caution: basically works
The case when this does not work is when the user presses the address bar and presses enter . That is, the server will generate a false positive, detecting an update, when there is a possibility, the user actually wanted to reload the page fresh.
Depending on your goals, this may be desirable, but as a user it would drive me crazy if I expected it to reset on the page.
I did not think too much about this, but it would be possible to write some kind of magic to distinguish the update from reset through the address bar using any / all:
SessionState (assuming SessionState enabled), and the value of the loaded QueryString parameterwindow.onbeforeunload event listener- keyboard events (detect F5 and Ctrl + R to quickly change the URL back to remove the
loaded QueryString parameter), although it would be false negative for the browser refresh button to be pressed) - biscuits
If someone comes up with a solution, I would like to hear it.
source share