简体   繁体   English

如何检测后退按钮单击的动作

[英]How to detect back button clicked action

Is it possible to detect whether back button was clicked or not using pure java, servlet, jsp. 是否可以使用纯Java,Servlet,JSP检测是否单击了后退按钮。 I saw few answers dealing with ajax, javascript etc. 我看到了一些有关ajax,javascript等的答案。

I was trying to detect the action in doGet method of my controller. 我试图检测控制器的doGet方法中的操作。

But that doesn't work. 但这是行不通的。 Noob question. 新手问题。

No. 没有。

Back button clicks happen on the client side. 后退按钮单击发生在客户端。

There's zero way for the server side to know unless you tell it from the client side. 除非您从客户端告知,否则服务器端知道的方式为零。

The event is fired on the client's side, so in order to handle it you need to use a client-side language. 该事件是在客户端触发的,因此,为了处理该事件,您需要使用客户端语言。 If you intercept such event on the client's side, for example with JavaScript, you can notify your back-end to behave in a certain way. 如果您在客户端(例如使用JavaScript)拦截此类事件,则可以通知后端以某种方式运行。

For example with JavaScript you can do 例如,您可以使用JavaScript

document.getElementById("backButton").addEventListener("click", function(){
    //notify your back-end here
});

of if you mean the browser back button 如果您是指浏览器后退按钮

window.onbeforeunload = function(){
    //check if it was a back-button press using history
    //notify back-end
}

So the answer is no, you can't intercept client-side events with a server-side language . 因此,答案是否定的,您无法使用服务器端语言拦截客户端事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM