简体   繁体   中英

How to detect back button clicked action

Is it possible to detect whether back button was clicked or not using pure java, servlet, jsp. I saw few answers dealing with ajax, javascript etc.

I was trying to detect the action in doGet method of my controller.

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.

For example with JavaScript you can do

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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