Get previous page

How to get the previous page with javascript by phone?

I want to return ( window.history.back() ) only if I came from one specific html file, otherwise I want to go to another html page when I click the "Back" button. So I want to know which one is the previous page of the story.

I was looking for time to search, and I did not find an answer that suits me.

+1
source share
1 answer

If you want something inline, you can use document.referrer to get the previous URL. But it can be unreliable for your needs. More details here , here and here

Another VERY simple option is to save the name of the current page in the session store, read it, and then decide what to do next.

 //retrieve the previous page var previouspage = window.sessionStorage.getItem("page"): // if thee is no previous page you are on the first page if (previousPage == undefined){ //do something if you want, this means it the first page the user is seeing } //get current page address var currentPage = window.location.href; //store it window.sessionStorage.setItem("page",currentPage); //check if the previous page is the one you wanted. if (previousPage == "the_page_you_wanted"){ // do something. } 

LocalStorage vs SessionStorage

+9
source

Source: https://habr.com/ru/post/971178/


All Articles