In Vue component, I want to handle on browser back event like this:
mounted() {
if ([browser back]) {
console.log("browser back button clicked")
} else {
console.log("stay here")
}
}
To handle browser back event, I found window.onpopstate
function but I don't know how to put it inside the if
statement.
Can you tell me what should I do on this case? Thank you!
You don't need to put inside an if statement. The event handler is sort of an "if statement".
See this example:
mounted() {
// if back button is pressed
window.onpopstate = function(event) {
alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With