开发者

Detecting User pressing back button without Javascript?

开发者 https://www.devze.com 2023-02-18 07:12 出处:网络
I know there are ways to tell if an user has pressed the back button 开发者_高级运维using Javascript, but is there a way to tell without resorting to Javascript? Is there a solution that involves just

I know there are ways to tell if an user has pressed the back button 开发者_高级运维using Javascript, but is there a way to tell without resorting to Javascript? Is there a solution that involves just looking at referer URLs perhaps?


Without javascript no.

The problem is the back button will not guarantee you get a server hit. It can cache the page client side and even if it did hit the server (loading the page), then it would have the request from the initial hit not like it came from the page you were just on. The back button doesn't add 'referral' information to the request. It just goes back to the last thing you did without sending the details of where you just were.

You need to handle this client side.


Yes, it is possible. There are two parts to this -

  1. Every URL should have a unique token in it. On the server side, you keep track of the current and past tokens. When a request comes along, if the token matches a past token, the back button was hit. If it equals the current token, process the request normally. Otherwise fail the request.
  2. Now, the page could have been cached and your server may not see the request. So you have to take steps to defeat the browser cache. Setting the following http headers should force the browser to make a request -

    Cache-Control : no-Cache
    Pragma : No-Cache
    Expires : Thu, 01 Jan 1970 00:00:00 GMT

Because it is possible doesn't mean you should use it though. Backbutton is an essential technique for the web, and breaking it is poor usability.

0

精彩评论

暂无评论...
验证码 换一张
取 消