How to get the previous URL in JavaScript?

后端 未结 7 773
野趣味
野趣味 2020-11-22 06:05

Is there any way to get the previous URL in JavaScript? Something like this:

alert(\"previous url is: \" + window.history.previous.href);

相关标签:
7条回答
  • 2020-11-22 06:12
    <script type="text/javascript">
        document.write(document.referrer);
    </script>
    

    document.referrer serves your purpose, but it doesn't work for Internet Explorer versions earlier than IE9.

    It will work for other popular browsers, like Chrome, Mozilla, Opera, Safari etc.

    0 讨论(0)
  • 2020-11-22 06:13

    Those of you using Node.js and Express can set a session cookie that will remember the current page URL, thus allowing you to check the referrer on the next page load. Here's an example that uses the express-session middleware:

    //Add me after the express-session middleware    
    app.use((req, res, next) => {
        req.session.referrer = req.protocol + '://' + req.get('host') + req.originalUrl;
        next();
    });
    

    You can then check for the existance of a referrer cookie like so:

    if ( req.session.referrer ) console.log(req.session.referrer);
    

    Do not assume that a referrer cookie always exists with this method as it will not be available on instances where the previous URL was another website, the session was cleaned or was just created (first-time website load).

    0 讨论(0)
  • 2020-11-22 06:16
    document.referrer
    

    in many cases will get you the URL of the last page the user visited, if they got to the current page by clicking a link (versus typing directly into the address bar, or I believe in some cases, by submitting a form?). Specified by DOM Level 2. More here.

    window.history allows navigation, but not access to URLs in the session for security and privacy reasons. If more detailed URL history was available, then every site you visit could see all the other sites you'd been to.

    If you're dealing with state moving around your own site, then it's possibly less fragile and certainly more useful to use one of the normal session management techniques: cookie data, URL params, or server side session info.

    0 讨论(0)
  • 2020-11-22 06:24

    document.referrer is not the same as the actual URL in all situations.

    I have an application where I need to establish a frameset with 2 frames. One frame is known, the other is the page I am linking from. It would seem that document.referrer would be ideal because you would not have to pass the actual file name to the frameset document.

    However, if you later change the bottom frame page and then use history.back() it does not load the original page into the bottom frame, instead it reloads document.referrer and as a result the frameset is gone and you are back to the original starting window.

    Took me a little while to understand this. So in the history array, document.referrer is not only a URL, it is apparently the referrer window specification as well. At least, that is the best way I can understand it at this time.

    0 讨论(0)
  • 2020-11-22 06:25

    This will navigate to the previously visited URL.

    javascript:history.go(-1)
    
    0 讨论(0)
  • 2020-11-22 06:31

    If you are writing a web app or single page application (SPA) where routing takes place in the app/browser rather than a round-trip to the server, you can do the following:

    window.history.pushState({ prevUrl: window.location.href }, null, "/new/path/in/your/app")
    

    Then, in your new route, you can do the following to retrieve the previous URL:

    window.history.state.prevUrl // your previous url
    
    0 讨论(0)
提交回复
热议问题