简体   繁体   中英

back button to first visited page

Apologies if my question sounds like a newbie question...but I could not find an appropriate answer.

I am trying to create 2 back buttons on my website that would bring the visitor back : 1. to the very first page he visited on my website 2. to the previous site (not page) he visited - so the site that brought him to my site basically.

Is this feasible by any chance ? And if so, can you please share your insights on the way to do it ?

Many thanks in advance for any help !

Maybe you can do something like this:

$(document).ready(function() {
   var sessionIsNew = sessionStorage.getItem('sessionIsNew');

   if (sessionIsNew == null) {
      sessionStorage.setItem('sessionIsNew', 'false');
      sessionStorage.setItem('entryUrl', window.location.href);
   }

}

// On click to your back button:
function backToEnrtryPage () {
    entryUrl  = sessionStorage.getItem('entryUrl');

    if (entryUrl != null) {
       window.location.href = entryUrl;
    }
}

More information about session in JavaScript

https://developer.mozilla.org/de/docs/Web/API/Window/sessionStorage

You can do it by manupulating the browser history, so to move once backward through history, just do:

window.history.back();

to back to initial entry page you can do somting like

var numberOfEntries = window.history.length;
window.history.go(- numberOfEntries );

the full api documentation from mozilla : https://developer.mozilla.org/en-US/docs/Web/API/History_API

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