Ahmet AY
Ahmet AY

Reputation: 391

How to recognise back action in html page?

I want to call a method from a class (jsf bean) when user leave the page by clinking back button of browser. I tried to use window.onbeforeunload, however it works for every situation. For example when user redirected with a link or button. How can I make it just for back action?

Upvotes: 0

Views: 80

Answers (1)

Ali Naci Erdem
Ali Naci Erdem

Reputation: 435

What you need is the history API.

https://developer.mozilla.org/en-US/docs/Web/API/History_API

window.onpopstate = function(event) {
  console.log("location: " + document.location + ", state: " + JSON.stringify(event.state));
};

Upvotes: 2

Related Questions