[javascript] How to make an HTML back link?

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.

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

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment Wrapping a react-router Link in an html button How to make a hyperlink in telegram without using bots? React onClick and preventDefault() link refresh/redirect? How to put a link on a button with bootstrap? How link to any local file with markdown syntax? link with target="_blank" does not open in new tab in Chrome How to create a link to another PHP page How to determine the current language of a wordpress page when using polylang? How to change link color (Bootstrap) How can I make a clickable link in an NSAttributedString?