How do I get if there is a previous page? (judgment of the return button)

problem description

pages entered directly through the url address need to cancel the return button. How to determine whether the current page is the first page when entering?

tried methods

1. Window.history
uses window.history.length to determine whether there is a higher-level page, which is valid when you first enter this page, but it still appears when you return from the lower-level page. Although you return to the current page, the window.history.length is already 2.
No return button appears when entering the page for the first time:

:

II, document.referrer
my project is a vue project, so the document.referrer is always empty, resulting in no return button for the subordinate page.

related codes

if (window.history.length <= 1) {
    this.hasReturn = false;
}
Apr.13,2022

< H2 > Preface < / H2 >

if you use a single page, document.referrer will always be empty (there is a value for .html or ssr pages)

History.length indicates the number of user history session pages. When a user loads a page with a value of "1" from a new tab or frame, the value increases by "1" for each page visited.
because the number of history.length is only increasing, it cannot be used to judge at all.

< H2 > scenario < / H2 >

it needs to be judged that the user opens the current web page through the browser from Wechat browser. If so, when the user clicks the back button, the user returns to the home page of the web application (site)

  • or: determine whether there is a previous page

    • through history.length

      • , if the user has ever opened it. Then there will be more history.length
      • if the user has not opened it and then opens it directly from the Wechat browser using the system browser, then history.length = 1 (where 1 in the mobile browser The chrome console is 2)
  • or: determine whether the user has visited

  • or: determine whether the specified url

    • does not find the relevant method
    • / ul >
    • or: determine whether to wake up the device browser through the Wechat browser and then open it to the current web page

      • did not find the relevant method

    there must be other ways to achieve return to the home page of the web application (site) when the user clicks the return button. Now my head is a little dizzy. If there is any, I would like to be shared. Thank you very much.
    by the way, can Vuex judge? There is really no relevant (built-in) method, maybe. But even if not, it doesn't matter, just expand one by yourself.

    Vue players can also refer to https://codeshelper.com/q/10...

    and < / H2 >

    Don't forget to listen for the returned events of the browser:

    window.addEventListener("popstate", function(e) {
      alert("");//
    }, false);
    < H2 > how do I solve this < / H2 >

    [dog head]
    when the user opens the web application (site) in Wechat, the user is prompted to use the browser to open it.
    together with Cookie and sessionStorage caches the router record to determine whether the user needs to return to the home page directly when clicking the return button;


    when returning, try using this.$router.replace


    to write two jumps directly. If you can return to the previous page, you will not push to the home page. If you do not return to the previous page, you will directly jump to the home page.

            this.$router.go(-1);
            this.$router.push({name: 'home'});

Menu