How to make an HTML back link?

后端 未结 12 804
臣服心动
臣服心动 2020-12-12 08:32
相关标签:
12条回答
  • 2020-12-12 09:14

    You can also use history.back() alongside document.write() to show link only when there is actually somewhere to go back to:

    <script>
      if (history.length > 1) {
        document.write('<a href="javascript:history.back()">Go back</a>');
      }
    </script>
    
    0 讨论(0)
  • 2020-12-12 09:18

    And another way:

    <a href="javascript:history.back()">Go Back</a>

    0 讨论(0)
  • 2020-12-12 09:18

    This solution has the benefit of showing the URL of the linked-to page on hover, as most browsers do by default, instead of history.go(-1) or similar:

    <script>
        document.write('<a href="' + document.referrer + '">Go Back</a>');
    </script>
    
    0 讨论(0)
  • 2020-12-12 09:20

    This solution gives you the best of both worlds

    • Users get to hover over the link to see the URL
    • Users don't end up with a corrupted back-stack

    More details in the code comments below.

    var element = document.getElementById('back-link');
    
    // Provide a standard href to facilitate standard browser features such as 
    //  - Hover to see link
    //  - Right click and copy link
    //  - Right click and open in new tab
    element.setAttribute('href', document.referrer);
    
    // We can't let the browser use the above href for navigation. If it does, 
    // the browser will think that it is a regular link, and place the current 
    // page on the browser history, so that if the user clicks "back" again,
    // it'll actually return to this page. We need to perform a native back to
    // integrate properly into the browser's history behavior
    element.onclick = function() {
      history.back();
      return false;
    }
    <a id="back-link">back</a>

    0 讨论(0)
  • 2020-12-12 09:21

    The best way using a button is

    <input type= 'button' onclick='javascript:history.back();return false;' value='Back'>

    0 讨论(0)
  • 2020-12-12 09:23

    A back link is a link that moves the browser backwards one page, as if the user had clicked the Back button available in most browsers. Back links use JavaScript. It moves the browser back one page if your browser supports JavaScript (which it does) and if it supports the window.history object, which is necessary for back links.

    Simple ways are

    <a href="#" onClick="history.go(-1)">Go Back</a>
    

    OR:

    function goBack() {
      window.history.back()
    }
    <a href="#" onclick="goBack()" />Go Back</a>

    Generally speaking a back link isn't necessary… the Back button usually suffices quite nicely, and usually you can also simply link to the previous page in your site. However, sometimes you might want to provide a link back to one of several "previous" pages, and that's where a back link comes in handy. So I refer you below tutorial if you want to do in more advanced way:

    http://www.htmlcodetutorial.com/linking/linking_famsupp_108.html

    0 讨论(0)
提交回复
热议问题