Search code examples
javascripturlbrowser-history

Get the URL of the previous page even after history.back() event


Is there a way I can get the URL of the last page the user has been? document.referrer isn't what I want, because it only gets the referrer and it won't work if, for instance, the user clicks the back button.

I suspect this isn't possible, since you can't even get the URL of the forward page in the history.


Solution

  • As indicated by @ctwheels, this seems to solve my problem: http://w3schools.invisionzone.com/index.php?showtopic=31256&p=169916

    Quoting from the page:

    get the url of just the last previous page? of your domain only, or all previous pages? of yours and external domains?

    if you require last previous page, of your domain only, would storing url in javascript cookie work?

    one cookie to store previous url, and another to store current, then next page, the current would become the previous.

    EDIT: @DontVoteMeDown recommended to use localStorage instead of cookies.