1

There are two pages, and I want to show an alert on the first page when the user comes back from second page to first page.

I know how to go back to first page by: window.location.href

and also by: window.history.back().

But I don't know how to show an alert once it's backed?

Yasin
  • 103
  • 10
  • Take a look at this: https://stackoverflow.com/questions/37838430/detect-if-page-is-load-from-back-button – WillD Apr 09 '20 at 14:22

2 Answers2

0

You can simply achieve it using localStorage or sessionStorage.

// Page 1

const visited = localStorage.getItem("visited")

if(visited) alert("//Do something here, why you came back")

else localStorage.setItem("visited", "visited")
xdeepakv
  • 7,835
  • 2
  • 22
  • 32
0

You can give the url a parameter and use it to check whether an alert message should be fired.

For example: window.location.search -> '?visited=true'

typable
  • 1
  • 2
  • 3