views:

281

answers:

2

As the title: there is a way in wich 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?

+3  A: 

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?

Bob
I'm in trouble with the messages registered server-side (asp.net). A user compile a form, send to the server, server do its serverthing and register back a javascript alert for the invalid things it founds.Then, if the user navigate back to history and forward again, the message appears another time.I think that if I can register a js function to check this situation I can bypass or not the alert.I don't know if your solution can works in my scenario, but I'll try :) thanks
tanathos
+2  A: 

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?

~Chris

cimnine