Understand (via JavaScript) if the current page is reached using browser's history
As the title: there is a way in which I can know, client side, if the current page is shown by navigating the browser's history,开发者_高级运维 or following a link, or by a postback?
Technically you cannot read the user's browser history. This is over privacy concerns. You can however do things to figure out if the user was brought to your page via the page referrer. If it is postback you can output a server side variable.
To get the referrer in javascript is pretty easy
var referrer = document.referrer;
To determine if it is a postback (in C#)
var isPostback = <%= IsPostBack %>
The history issue is a little more complicated. The best way around that would be to set your pages to expire immediately.
Response.Cache.SetNoServerCaching();
Response.ExpiresAbsolute = DateTime.Now;
But these are relatively loose solutions. Why do you want this behavior?
You could add a date stamp to every page. If the timestamp is too far away, the user probably used the back button. But this won't work if the browser is not loading the page from cache.
Wait, you could add a timestamp to the url too. Now you compare the timestamp of the page with the timestamp of the referer url.
I'm thinking about a solution with cookies, but I can't get one right now. Someone?
精彩评论